When we work with strings or text data, it often involves printing a new line. Generally, we use the print statement to print a string and another print statement to print it in a new line. After execution, when we write the print statement, the cursor automatically shifts to a new line. However, if we keep on writing new print statements repeatedly for every string, the code will become unclear and absurd.
Hence, we use the escape character ‘\n’ to print a newline in Python. In this blog, we will discuss the different ways to print a new line, along with some examples.
Print a Newline Using the \n Escape Sequence
In Python, an escape sequence is a special character, providing a simple and common way to print a newline by using a backslash (\). It represents a certain character or behavior. Using escape sequences, we can include characters in a string that are difficult to type directly into the code.
We use escape sequences to add Python newline characters (\n) to a string. A newline character represents the end of a text line and causes the next character to be printed on a new line.
So, when a string has a newline character, Python will start a new line in the output. It is used to format text in a program or create multi-line strings.
Let’s understand this method through its functions, syntax, and more.
Character
'\n'
Purpose
To shift the cursor to a new line.
Features
- It is an escape sequence character.
- It is valid for strings and characters only.
- It is also known as line-break.
- We can add ‘\n’ anywhere in a string.
Syntax
1. Declare a string in multiple lines
Str_var = "\nstring 1\n string 2\n"
2. Print a string in multiple lines
print ("\nstr1\nstr2\n")
Initial \n- It leaves a blank line and shifts to a new line, printing str1.
Middle \n- It prints str1 and then shifts to a new line, printing str2.
Final \n- After executing str2, it leaves a blank line and shifts to a new line.
Also read: Python Developer Roadmap (Guide for Beginners)
Why do we need '\n'?
If you want to print “Hello”, “My”, and “Friends” in three different lines, you will do it using the normal print statement as shown below:
Code
print ("Hello")
print (“My”)
print ("Friends")
Output
Hello
My
Friends
So, you would require 3 lines of code to print these words. It is a longer process and may not be useful for printing more strings. Therefore, we use ‘\n’ as shown in the following code:
print ("Hello\nMy\nFriends")
Output
Hello
My
Friends
Here, you need to write one line of code to print a new line in Python. You can use ‘\n’ to print any number of strings in multiple lines while keeping the code precise and simple.
More About '\n'
Many wonder how Python recognizes ‘\n’ and why the print statement doesn’t print ‘\n’ like a normal string. Well, Python has a few predefined characters succeeding a backslash (\), known as the escape sequences. Python easily recognizes ‘\’, understands it’s not part of a string, and executes it according to the succeeding character.
When we use a backslash before a character, the character can escape the normal string execution. A few examples of these characters are \t, \r, \n, etc.
Example- Declaring a string with '\n'
message = "Hello\nPython\nWorld"
print(message)
Output:
Hello
Python
World
Recommended Professional Certificates
Full Stack Development Course with AI Engineering
WordPress Bootcamp
Frequently Asked Questions
Learn how to print a newline in Python with simple examples. Explore different methods to add line breaks in your code. This guide provides structured insights, proven methodologies, and actionable execution steps for modern professionals.
GrowthNestMedia provides end-to-end creative media solutions, AI integration, video ad production, and digital growth consulting tailored to scale your brand efficiently.
Scale Your Brand with GrowthNestMedia
From AI-powered video ads to high-converting websites and creative digital strategies, we help your business achieve measurable growth.
Get Started Today