Python Blank Lines
In Python, if we want a blank line in output or an empty line in output, we cannot get it by just writing a blank line in our code. For example, if we want a blank line between two outputs and we write like this:
Code:
print('First Line')
print('Second Line')
Output:
First Line
Second Line
We put a blank line in our code between the first and second print statements, but we didn’t get a blank line in the output. The second output just came after the first output. So to get an empty or blank line, there are different options in Python, which we can use.
print() to Get Blank Line in Python
In the upper example, we tried to get a blank line in the result by providing a blank line in our code. But that didn’t work. So now we are going to use a print() function to get blank lines in output.
Code:
print('First Line')
print()
print('Second Line')
Output:
First Line
Second Line
So we get a blank line between both outputs. If we use two-time print statements, then:
Code:
print('First Line')
print()
print()
print('Second Line')
Output:
First Line
Second Line
Now we get two blank lines in the output. So we used print()
; this print()
is called ‘blank print statement’ or ’empty print statement’. Because we didn’t use any argument with this print statement, that is why it’s called a blank print statement.
Now here is one more example:
Code:
print('First Line')
print(' ')
print('Second Line')
Output:
First Line
Second Line
We get the same result. print()
and print(' ')
are almost the same. print(' ')
is called print with blank argument. We used single quotes; if we want, we can use double quotes too.
Code:
print('First Line')
print(" ")
print('Second Line')
Output:
First Line
Second Line
Remember, when we use print(' ')
and print ("")
here, the open and close quotes should be the same. We cannot use different quotes for open and close. For example:
Code:
print('First Line')
print(' ")
print('Second Line')
Output:
SyntaxError: unterminated string literal
We get a SyntaxError because we are not following the rules for writing quotes in Python. So we should use the same quote for open and close.
Click For Python Quotes Tutorial
Escape Sequence to Get Blank Line in Python
Another way to get a blank line in Python is by using an escape sequence. Escape sequences are the special type of characters that we can use in Python to do some modification in output. To get a blank line in output, we used \n escape character. For example:
Code:
print('First line \n')
print('Second line')
Output:
First line
Second line
So we get easily a blank line in output. Now if we want two blank lines, then we use two time \n characters. For example:
Code:
print('First line \n\n')
print('Second line')
Output:
First line
Second line
So we get easily two blank lines in output.
So basically, \n escape character is used for a new line. So if we use this between in a single string line. We get multiple lines in the output. For example:
Code:
print('First line \nSecond line \nThird line')
Output:
First line
Second line
Third line
So we get our output in multiline.
Always remember, we used \n with strings means inside the quotes, if we used it outside the quote. It will not work. For example:
Code:
print('First line'\n)
print('Second line')
Output:
SyntaxError: unexpected character after line continuation character
So this is how we used the escape character to get a blank line in Python.
Loops to Get Blank Lines in Python
We can also use loops to get blank lines in Python. Loops are used for iteration in Python. When we want to repeat something again, we use loops. Here we are creating a small program to get blank lines in Python.
Code:
print('First Line')
for blankline in range(1):
print()
print('Second Line')
Output:
First Line
Second Line
So here we create a for loop with rang() function. range() is a built-in function in Python. So we set range(1), and then the blank print() statement. So it means we want to print a blank statement for one (1) time. If we set range(2) or range(3), then we get three blank lines in output.
Code:
print('First Line')
for blankline in range(3):
print()
print('Second Line')
Output:
First Line
Second Line
So basically here we are using blank print statements, but instead of writing print statements multiple times, we simply set a program with a loop.
Conclusion
This is the end of the Python blank lines tutorial. This is some basics of Python. Sometimes when we want a blank or empty line in output. We can use the above methods to do that.
End of Python Blank Lines Tutorial
One thought on “Python Blank Lines: How to Create Blank Lines in Python?”