2 ways to print a new line in Python:
- Using an Escape Sequence
- Using print() function
Method 1: Using an Escape Sequence
You can use the \n escape sequence to insert a newline character.
Visual Representation
Example 1: Single new line
print("This is a line of text.\nThis is a new line of text.")
Output
This is a line of text.
This is a new line of text.
Example 2: Multiple new lines
You can also use \n multiple times to add multiple empty lines or format your output as needed.
print("This is a line of text.\n\nThis is a new line of text.")
Output
This is a line of text.
This is a new line of text.
Method 2: Using print() function
The print() function, by default, adds a newline character at the end of the line.
You can also specify the end parameter (default is “\n”) to customize the default line-ending character.
Visual Representation
Example
print("This is a line of text.")
print("This is a new line of text.")
Output
This is a line of text.
This is a new line of text.
If you want to add a blank line between two lines of text, use an empty print() statement.
print("This is a line of text.")
print()
print("This is a new line of text.")
Output
This is a line of text.
This is a new line of text.