Table of Contents
The print()
function can display some message or output in Python. Sometimes, the final result can be congested and unclear due to a need to display multiple or long outputs.
What is a blank line in Python?
We can use a blank line to display the final output in a clean, understandable way. A blank line is an empty line between two messages. The \n
character produces a newline in Python.
We will understand how to print blank lines in this tutorial.
Ways to print blank line in Python
Using an empty print()
function
In Python, multiple uses of the print()
function can display the output in different lines.
See the following example.
1 2 3 4 |
print("This is the first message") print("This is the second message") |
Output:
This is the second message
This is because the print()
function has a parameter end
, which is \n
by default. The end
parameter adds this character to the end of the output. So, a blank line can be displayed using an empty print()
function.
For example,
1 2 3 4 5 |
print("This is the first message") print() print("This is the second message") |
Output:
This is the second message
Further reading:
Using the print()
function with newline character
In the print()
function, we can add an empty line between messages using the newline character \n
within the function. We will use the newline character twice to add a blank line between two messages.
For example,
1 2 3 |
print("This is the first message\n\nThis is the second message") |
Output:
This is the second message
Using the print()
function with an empty string
We can specify an empty string using single quotes in the print()
function to display a blank line.
See the code below.
1 2 3 4 5 |
print("This is the first message") print('') print("This is the second message") |
Output:
This is the second message
Similarly, we can specify an empty string using double quotes in the print()
function to display a blank line.
For example,
1 2 3 4 5 |
print("This is the first message") print("") print("This is the second message") |
Output:
This is the second message
Using the print()
function with the sep
parameter
The sep
parameter in the print()
function specifies the character that separates two values in Python. Its default value is blank whitespace.
If we change this to two newline characters \n
, we can add a blank line between values.
See the code below.
1 2 3 |
print("This is the first message","This is the second message", sep = '\n\n') |
Output:
This is the second message
Ways to print multiple blank lines in Python
Using the print()
function with multiple newline characters
As discussed earlier, we can use the newline character \n
to add a blank line between messages in Python. We can use it multiple times to display multiple blank lines.
For example,
1 2 3 |
print("This is the first message \n\n\n\n\n\n\n This is the second message") |
This is the second message
Using the print()
function with multiplication operator
Another way to achieve this is by using the multiplication operator *
. We multiply the newline character \n
the required number of times to display multiple blank lines.
For example,
1 2 3 |
print("This is the first message",'\n'*5,"This is the second message") |
Output:
This is the second message
Conclusion
We have explained and discussed different methods to print blank line in Python in this article. Such blank lines can help in displaying a clean output that is readable and understandable.