Print new line after a variable in Python

avatar

Borislav Hadzhiev

Last updated: Sep 5, 2022

banner

Photo from Unsplash

Print new line after a variable in Python #

Use the addition operator to print a new line after a variable, e.g. print(variable + '\n'). The newline (\n) character is a special character in python and is used to insert new lines in a string.

main.py
# ✅ Print new line after variable variable = "bobby" my_str = variable + '\n' + 'hadz' # bobby # hadz print(my_str) # ---------------------------------- # ✅ Print new line after each item in list of strings # 👇️ print items of list of strings on separate lines my_list = ['bobby', 'hadz', 'com'] result = '\n'.join(my_list) # bobby # hadz # com print(result) # ---------------------------------- # ✅ Print new line after each item in list of integers my_list = [2, 4, 8] result = '\n'.join(str(num) for num in my_list) # 2 # 4 # 8 print(result)

The first example uses the addition (+) operator to print a new line after a variable.

main.py
variable = "bobby" my_str = variable + '\n' + 'hadz' # bobby # hadz print(my_str)

If the variable is not of type string, pass it to the str() class before using the addition operator.

Alternatively, you can use a formatted string literal.

main.py
variable = "bobby" my_str = f'{variable}\nhadz' # bobby # hadz print(my_str)
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

When using a formatted string literal, you don't have to convert the value stored in the variable to a string as this is done for us automatically.

If you need to print a new line after each item in a list, use the str.join() method to join the list with a newline (\n) character separator.

main.py
my_list = ['bobby', 'hadz', 'com'] result = '\n'.join(my_list) # bobby # hadz # com print(result)

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your iterable contains numbers or other types, convert all of the values to strings before calling join().

main.py
my_list = [2, 4, 8] result = '\n'.join(str(num) for num in my_list) # 2 # 4 # 8 print(result)

The string the method is called on is used as the separator between the elements.

Alternatively, you can use a for loop to iterate over the list and print each item.

main.py
my_list = ['bobby', 'hadz', 'com'] for item in my_list: # bobby # hadz # com print(item)

By default, the print() function prints a newline character at the end of each message.

You can change this behavior by setting the end argument in the call to print().

main.py
print('a', 'b', 'c') # 👉️ 'a b c\n' print('a', 'b', 'c', end='') # 👉️ 'a b c'
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.