Adding a blank line in your Python code can make it more readable and easier for others to understand, especially when working with large blocks of text or code. In this tutorial, we will learn how to add a blank line in Python using various methods, such as the print() function, escape characters, and triple quotes.
Method 1: Using the print() function
The simplest way to add a blank line in Python is by using the print() function without any arguments. It will print a single newline character. For example:
1 2 3 |
print("Hello, World!") print() print("Let's learn Python!") |
Output:
Hello, World! Let's learn Python!
Method 2: Using escape character (\n)
The escape character \n represents a newline in a string. You can use it to add a blank line within a string or between two strings, like so:
1 |
print("Hello,\n\nWorld!") |
Output:
Hello, World!
You can also use it between two strings while adding a blank line.
1 |
print("Hello, World!" + "\n" + "Let's learn Python!") |
Output:
Hello, World! Let's learn Python!
Method 3: Using triple quotes (”’ ”’ or “”” “””)
Triple quotes are used in Python to create multiline strings. They can also be used to add a blank line within a string or between two strings, like so:
1 2 3 |
print('''Hello, World!''') |
Output:
Hello, World!
You can also use it to add a blank line between two separate print statements.
1 2 3 4 5 6 |
print("Hello, World!") print(''' ''') print("Let's learn Python!") |
Output:
Hello, World! Let's learn Python!
Full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Using print function print("Hello, World!") print() print("Let's learn Python!") # Using escape character print("Hello,\n\nWorld!") print("Hello, World!" + "\n" + "Let's learn Python!") # Using triple quotes print('''Hello, World!''') print("Hello, World!") print(''' ''') print("Let's learn Python!") |
Conclusion
In this tutorial, we learned three different methods to add a blank line in Python. Each method is helpful depending on the context in which it’s used. The print() function is the simplest to use while escaping characters and triple quotes provide more flexibility when working with strings.