How to print new lines in Python


Borislav Hadzhiev

Last updated: Sep 5, 2022


Photo from Unsplash

Print new lines in Python #

Use the \n character to print new lines, e.g. my_str = "bobby\nhadz\ncom". The newline (\n) character is a special character in Python and is used to insert new lines in a string.
# ✅ Print a new line using \n my_str = "bobby\nhadz\ncom" # bobby # hadz # com print(my_str) # ------------------------------ # ✅ Print a new line using \n after a variable variable = "bobby" my_str = variable + '\n' + 'hadz' # ------------------------------ # ✅ Print a new line using a multiline string my_str = """\ bobby hadz com""" # bobby # hadz # com print(my_str)

The first example uses the \n character to print new lines.
my_str = "bobby\nhadz\ncom" # bobby # hadz # com print(my_str)

If you have a value stored in a variable, use the addition (+) operator to add a new line after the variable.
variable = "bobby" my_str = 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 print new lines by using a multiline string.
my_str = """\ bobby hadz com""" # bobby # hadz # com print(my_str)

Triple-quotes strings are very similar to basic strings that we declare using single or double quotes.

But they also enable us to:

  • use single and double quotes in the same string without escaping
  • define a multi-line string without adding newline characters

End of lines are automatically included in triple-quoted strings, so we don't have to add a newline character at the end.

By default, the print() function inserts a newline (\n) character after the message.

You can set the end argument to an empty string if you need to remove the trailing newline character.
my_str = "bobby\nhadz\ncom" # bobby # hadz # com% print(my_str, end='')

The string we passed for the end keyword argument is inserted at the end of the string.

If you need to print the items of a list on new lines, use the str.join() method to join the list with a newline character separator.
# 👇️ 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 items of list of integers on separate lines my_list = [1, 2, 3] result = '\n'.join(str(num) for num in my_list) # 1 # 2 # 3 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.

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

An alternative to using the \n character is to use the os.linesep attribute.
import os my_str = f"bobby{os.linesep}hadz{os.linesep}com" # bobby # hadz # com print(my_str)

The os.linesep attribute returns the string that is used to separate lines on the current platform.

For example \n on Unix and \r\n on Windows.

Newline characters are automatically added after each line in a file.

So if you have a simple txt file that looks like the following:

bobby hadz com

You can use the file.readlines() method to get a list of the lines in the file.
with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() print(lines) # 👉️ ['bobby\n', 'hadz\n', 'com'] # bobby # hadz # com for line in lines: print(line, end='')

Notice that a newline character is automatically inserted after each line in the file.

When writing to a file, you can use the newline (\n) character to insert a new line.
with open('example.txt', 'w', encoding='utf-8') as my_file: my_file.write('bobby' + '\n') my_file.write('hadz' + '\n') my_file.write('com' + '\n')

This works in the same way it works with strings.

The file's contents look like:

bobby hadz com
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.