Python Line Break
Introduction
In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an existing one. A string with line breaks can be created using any of the following techniques.
- Newline code \n(LF), \r\n(CR + LF).
- Triple quote”’ or “””.
- With indent.
Let's study the line break instruction in greater detail.
Line Break in Python
Python requires either parentheses or an explicit backslash(/) to split a line. You could write over many lines by utilizing parentheses. Using Python's inferred line continuation inside brackets, parentheses, and braces is the recommended method for wrapping lengthy lines.
By encapsulating expressions in parenthesis, lengthy lines can be split into numerous lines. These should be utilized instead of a backslash to continue a line.
Let's utilize Python's parentheses to create a line break.
Code:
# Initializing the variables
a = 11
b = 22
c = 33
d = 44
e = 55
data = (a + b + c +
d + e)
# Print out the data
print (data)
Output:
[Running] python -u "d:\Programming\Python\test.py"
165
[Done] exited with code=0 in 0.372 seconds
You see how we calculated the total of each variable.
In order to divide the code into numerous lines without producing an error, we can alternatively use the specific line break (\).
Code:
# Initializing the variables
a = 11
b = 22
c = 33
d = 44
e = 55
data = (a + b + c + \
d + e)
# Print out the data
print (data)
Output:
[Running] python -u "d:\Programming\Python\test.py"
165
[Done] exited with code=0 in 0.34 seconds
And it will produce the same result without making a mistake.
Depending on the situation, you can use parentheses or a backslash, but while doing so, use caution. The risk of using a backslash to finish a line is that it will no longer function as intended if whitespace is introduced after the backslash (which, of course, is very difficult to discern).
Briefly, lines can be broken between parenthesis and braces. Additionally, you can specifically break a line by adding the backslash symbol "/" to it.
Python New Line Character
Python's new line character is n. It consists of just two characters:
- A backslash(\).
- The letter n.
This character in a string marks the end of the current line, and a new line starts immediately after it.
Printing commands by default insert a new line character at the end of a string.
print("Expeli Armus")
The string is prefixed with a new line character because the built-in print function's end parameter's default value is n.
New Line Character Statements in Print
Printing commands by default insert a new line character at the end of a string.
Like this:
print ("Hello, World! \n")
This happens as a result of, per the Python documentation:
The string is appended (means "add to the end".) with a new line character because the built-in print function's end parameter's default value is \n.
This is the function definition:

The string will have this added to the end because the end has the value n.
If you only use one print statement, only one line will be displayed so you won't notice this:
print (“Hello, World!”)
Output:
[Running] python -u "d:\Programming\Python\test.py"
Hello, World!
[Done] exited with code=0 in 0.364 seconds
However, if you repeatedly utilize print statements in a Python script:
print ("Hello, World!")
print ("Hello, World!")
print ("Hello, World!")
print ("Hello, World!")
print ("Hello, World!")
print ("Hello, World!")
Output:
[Running] python -u "d:\Programming\Python\test.py"
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
[Done] exited with code=0 in 0.306 seconds
Because n has been appended "behind the scenes" to the end of every line, the output will be displayed in separate lines:
How to display without a New Line
By altering the value of the print function's end parameter, we can override this default behavior.
If we apply the default setting to this instance:
print ("Hello")
print ("World")
Output:
The output is displayed in two lines
[Running] python -u "d:\Programming\Python\tempCodeRunnerFile.py"
Hello
World
[Done] exited with code=0 in 0.383 seconds
However, if we alter the end value and set it to " "
print ("Hello", end=" ")
print ("World!")
Output:
[Running] python -u "d:\Programming\Python\test.py"
Hello World!
[Done] exited with code=0 in 0.301 seconds
The result of the two print statements will appear on the same line because the new line character n will be substituted with a space at the end of the string:
This can be used, like in the following example, to print a series of values on a single line:
for i in range (15):
if I < 14:
print (i, end=" ,")
else:
print (i)
The output is:
[Running] python -u "d:\Programming\Python\test.py"
0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,14
[Done] exited with code=0 in 0.367 seconds
Tip: To ensure that the comma won't be added to the sequence's final number, we add a conditional expression.
In the same manner, we can employ this to print an iterable's values on the same line:
data = [7, 4, 6, 3, 2, 8, 1, 5]
for num in range (len(data)):
print (num, end=" ")
Output:
[Running] python -u "d:\Programming\Python\test.py"
0 1 2 3 4 5 6 7
[Done] exited with code=0 in 0.36 seconds
The Files' New Line Character
Although it is "hidden," the new line character \n can also be discovered in files. A new line character \n has been added whenever you see one in a text file.

This can confirm by reading the file with <file>.readlines(), as follows:
with open ("Name.txt", "r") as f:
print(f.readlines())
Output:
[Running] python -u "d:\Programming\Python\test.py"
['Gino\n', 'Nora\n', 'Talina\n', 'Gerard']
[Done] exited with code=0 in 0.33 seconds
You can see that the text file's first three lines each end with the "behind the scenes" new line n character.
Tip: Note that only the file's final line ends without a new line character.
Conclusion
Python's new line character is n. It serves as a textual cue that a line of text has ended.
Without adding a new line using end = character>, You can print strings, which indicate the character to be used to break the lines.
I truly hope you liked and learned something from my post. The new line character in python is now usable.