Print New Line after a Variable in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Print a new line after a variable in Python
  2. Print a new line after a variable using a formatted string literal
  3. Using a triple-quoted string to print new lines in Python
  4. Print a newline after each list item in Python
  5. Print a newline after each list item with a list of integers
  6. Print a newline after each list item using a for loop
  7. Using os.linesep instead of \n to print newlines in Python

# Print a 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
variable = "bobby" my_str = variable + '\n' + 'hadz' # bobby # hadz print(my_str)

print new line after variable

The code for this article is available on GitHub

The 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.

# Print a new line after a variable using a formatted string literal

Alternatively, you can use a formatted string literal.

main.py
variable = "bobby" my_str = f'{variable}\nhadz' # bobby # hadz print(my_str)

print new line after variable using f string

The code for this article is available on GitHub
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.

# Using a triple-quoted string to print new lines in Python

Alternatively, you can print new lines by using a multiline string.

main.py
variable = "bobby" my_str = f"""\ {variable} hadz com""" # bobby # hadz # com print(my_str)

using triple quoted string to print new lines

The code for this article is available on GitHub

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 multiline 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.

# Print a newline after each list item in Python

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)

print newline after each list item

The code for this article is available on GitHub

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.

# Print a newline after each list item with a list of integers

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.

# Print a newline after each list item using a for loop

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)
The code for this article is available on GitHub

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'

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

# Using os.linesep instead of \n to print newlines in Python

An alternative to using the \n character is to use the os.linesep attribute.

main.py
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:

example.txt
bobby hadz com

You can use the file.readlines() method to get a list of the lines in the file.

main.py
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='')
The code for this article is available on GitHub

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.

main.py
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 as follows.

example.txt
bobby hadz com

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev