zaro

How do you print multiple new lines in Python?

Published in Python Printing 2 mins read

You can print multiple new lines in Python by including multiple newline characters (\n) within a string, or by using multiple print statements.

Here's a breakdown of different methods:

1. Using Multiple \n Characters within a String

The most straightforward way is to embed multiple newline escape sequences (\n) directly into your string. Each \n represents a single new line.

print("This is line 1.\n\n\nThis is line 4.")

This code will produce the following output:

This is line 1.



This is line 4.

As you can see, there are three new lines separating "This is line 1." and "This is line 4." because of the three \n characters.

2. Using Multiple print() Statements

Another way to achieve multiple new lines is by using multiple print() statements. Each print() statement, by default, adds a newline character at the end of its output.

print("This is line 1.")
print()  # Prints a blank line (newline)
print()  # Prints another blank line (newline)
print("This is line 4.")

This code generates the same output as the previous example:

This is line 1.



This is line 4.

3. Using end Parameter

The print() function has an optional end parameter, which defaults to \n. You can change this to customize what is printed at the end of the print() statement. To print multiple newlines, you can modify the end parameter accordingly:

print("This is line 1.", end="\n\n\n") #Print three new lines
print("This is line 4.")

This will again give the same output:

This is line 1.



This is line 4.

Summary

In conclusion, printing multiple new lines in Python can be easily accomplished by including multiple newline characters (\n) within a string, using multiple print() statements, or customizing the end parameter of the print() function. The best approach depends on the specific context and desired level of readability in your code.