How to Print New Line after a Variable in Python [7 Ways]

In this Python tutorial, I will explain “How to print new line after a variable in Python using different methods and ways with some illustrative examples.

While working, I wondered how we could print a newline after a variable. Then, I came across seven different ways in my research to accomplish this, each with unique advantages and use cases.

Here is a list of all the ways to print new line after a variable in Python:

  1. Print a new line after a variable in Python
  2. With a triple-quoted string
  3. using a formatted string literal
  4. Printing newline after each element of the list.
  5. using a for-loop
  6. Print newline after lists of nested list
  7. Using os.linesep to print newlines

Let’s see them all one by one with examples in detail.

Print new line after a variable in Python

One straightforward method to print a new line after a variable in Python is using the print() function with the end parameter set to \n. This will ensure that the next output appears on a new line.

Consider the following example:

name = "John"
print("Hello, " + name, end='\n')
print("Welcome to the USA!")

Output:

Hello, John
Welcome to the USA!

Displayed below is the Python code alongside its output, captured in Visual Studio

Print New Line after a Variable in Python

Using a Triple-Quoted String to Print New Lines in Python

Triple-quoted strings allow us to include new lines directly within the string literal. This method is useful when we are dealing with multiline strings or when we want explicit control over line breaks in Python.

READ:  How to Create a List in Python [+5 Examples]

Here’s a simple example:

quote = """The United States of America,
often referred to as the USA or simply the States,
is a country primarily located in North America."""
print(quote)

Output:

The United States of America,
often referred to as the USA or simply the States,
is a country primarily located in North America.

Below is a screenshot of the Python code and its output in Visual Studio.

python add new line after variable

Python print new line after variable with formatted string literal

Formatted string literals, also known as f-strings, offer a concise and readable way to format strings in Python. We can include variables directly within the string and print new line after a variable in Python.

Let’s see an example

city = "New York"
print(f"I live in {city}. \nIt's a bustling city in the USA.")

Output

I live in New York. 
It's a bustling city in the USA.

Refer to the screenshot of the Python code and its output within Visual Studio

How to add newline In Python

Printing newline after each element of a list in Python

When we have a Python list with different items and want to print them individually, we can use the join() function to join the \n (newline) character to each element. Hence, we can easily print a new line after a variable in Python.

For instance:

states = ["California", "Texas", "New York"]
states = '\n'.join(states)
print(states)

Output

California
Texas
New York

Displayed below is the Python code alongside its output, captured in Visual Studio

How to get a new line after printing a variable

Python print new line after variable using for loop

When dealing with lists, we can iterate over each item and print them individually with a new line after each item by using a for loop in Python.

READ:  Python find max value in a dictionary [4 Methods]

Here’s how you can use it:

cities = ["Los Angeles", "Chicago", "Miami"]
for city in cities:
    print(city)

Output

Los Angeles
Chicago
Miami

Attached is a visual representation of the Python code along with its output within Visual Studio

Print New Line after a Variable in Python

Print newline after list of nested lists in Python

In such cases, where data will be stored as a nested list. We can solve this problem by using a nested for loop. It can iterate over every element inside lists of lists and print in a new line in Python.

Here’s a straightforward illustration:

employee_names = [['john','george'],['fred','jack']]

for i in employee_names:
    for j in i: 
        print(j)    

Output

john
george
fred
jack

Presented a screenshot of both the Python code and its output in Visual Studio

Print newline after lists of nested list in Python

Print new Line after a Variable in Python using os.linesep

For platform-independent newline characters, we can use the os.linesep constant in Python, which represents the appropriate newline character sequence for the current platform.

Here’s an example to illustrate the use of os.linesep in Python:

import os
name = "Emily"
print("Hello, " + name + os.linesep + "Welcome to the USA!")

Output

Hello, Emily
Welcome to the USA!

Here’s a combined view of the Python script and its output within Visual Studio

python print new line after variable

Conclusion

Here, we have discussed how to print a new line after a variable in Python through various methods, each with its own advantages.

From using the simple ‘\n’ parameter in the print function to employing triple-quoted strings, formatted string literals, or iterating through lists with for-loops, there are several approaches to accomplish this task efficiently.

Additionally, for platform-independent newline characters, the os.linesep constant provides a reliable solution.

You may also like to read: