How to Print a New Line After a Variable in Python?

While discussing the basic topics in Python, a member asked about printing a new line after a variable. There are various methods to do this. In this tutorial, I will explain various methods to print a new line after a variable in Python with real examples.

To print a new line after a variable in Python, you can simply use the print() function, which by default ends with a newline character. For example, if you have a variable name = "John", using print(name) will automatically print “John” followed by a new line.

Method 1: Using the print() Function

The best way to print a new line after a variable in Python is by using the print() function. By default, the print() function ends with a newline character.

Here is an example.

Example:

name = "John"
print(name)
print("Welcome to the USA!")

Output:

John
Welcome to the USA!

In this example, the print() function automatically adds a new line after printing the variable name.

You can also see the exact output in the screenshot below:

Print a New Line After a Variable in Python

Check out How to Print A Variable in Python?

Method 2: Using the end Parameter

The print() function has an optional end parameter that allows you to specify what should be printed at the end of the output. By default, end is set to '\n' (newline character). You can customize it to include additional new lines or other characters.

Example:

Here is a simple example and the complete Python code.

city = "New York"
print(city, end='\n\n')
print("The city that never sleeps.")

Output:

New York

The city that never sleeps.

In this example, we set end='\n\n' to add an extra blank line after printing the variable city.

You can see the output in the screenshot below:

python print new line after variable

Method 3: Using the sep Parameter

The print() function also has a sep parameter that specifies the separator between multiple arguments. You can use this to insert new lines between variables.

Let me show you an example to help you understand it better.

Example:

first_name = "Alice"
last_name = "Smith"
print(first_name, last_name, sep='\n')
print("Welcome to California!")

Output:

Alice
Smith
Welcome to California!

Here, the sep='\n' parameter ensures that each variable is printed on a new line.

Check out Print Variable Names in a For Loop in Python

Method 4: Using Escape Characters

Escape characters are special characters that allow you to include non-printable characters in strings. The newline character \n is commonly used to add new lines within strings in Python.

Here is an example of printing a new line after a variable in Python using escape characters.

Example:

greeting = "Hello, Alice!\nWelcome to Texas!"
print(greeting)

Output:

Hello, Alice!
Welcome to Texas!

In this example, the \n within the string greeting adds a new line between the two sentences.

Method 5: Using Triple Quotes for Multi-line Strings

Triple quotes (''' or """) allow you to create multi-line strings easily. This is useful for printing text that spans multiple lines without explicitly using newline characters.

Let me show you an example.

Example:

message = """Dear Bob,

Welcome to Florida!
We hope you enjoy your stay.

Best regards,
The Team"""
print(message)

Output:

Dear Bob,

Welcome to Florida!
We hope you enjoy your stay.

Best regards,
The Team

In this example, the triple quotes allow us to format the string across multiple lines naturally.

Read How to Print String and Variable in Python?

Method 6: Using String Concatenation

You can concatenate strings with newline characters to control the output format. This way, you can print a new line after a variable in Python.

Here is an example.

Example:

state = "California"
message = state + "\n" + "The Golden State"
print(message)

Output:

California
The Golden State

Here, we concatenate the variable state with a newline character and another string, ensuring that the output is printed on separate lines.

You can see the output in the screenshot below:

How to print new line after variable in Python

Conclusion

In this tutorial, I have explained how to print new lines after variables in Python using different methods like using the print() function’s end and sep parameters, escape characters, triple quotes, string concatenation, etc. I hope all the above examples help you to do so.

You may also like:

Leave a Comment