How to Print Newlines in Python
The newline character (\n
) is fundamental for controlling output in Python.
This guide explores various methods for adding newlines when printing, covering techniques from simple string concatenation and f-strings to multiline strings, joining list items, and using platform-specific newline conventions.
Printing Newlines with String Concatenation
The most basic method is to concatenate the newline character (\n
) with a string:
variable = "tutorial"
my_str = variable + '\n' + 'reference'
print(my_str)
Output:
tutorial
reference
- The
\n
character inserts a newline, causingreference
to print on a new line.
Printing Newlines with f-strings
F-strings provide a more concise way to embed newlines within strings:
variable = "tutorial"
my_str = f'{variable}\nreference'
print(my_str)
Output:
tutorial
reference
- The
\n
character within the f-string causes the line break. - f-strings automatically convert the value to a string, so no explicit conversion is necessary.
Printing Newlines with Triple-Quoted Strings
Triple-quoted strings preserve newlines literally, and allow the use of single and double quotes within the string without escaping:
variable = "tutorial"
my_str = f"""\
{variable}
reference
com"""
print(my_str)
Output:
tutorial
reference
com
- The backslash after the starting triple quote avoids an empty line at the top of the string.
- Triple quoted strings are useful if you need to create multi-line strings.
Printing Newlines After Each List Item
To print items from a list on separate lines, you can use the str.join()
method with \n
or use a for loop.
1. Using String Join
Use str.join()
to add a newline character between every item of a list:
my_list = ['tutorial', 'reference', 'com']
result = '\n'.join(my_list)
print(result)
Output:
tutorial
reference
com
If you have numbers in your list, you must first convert them to strings:
my_list = [2, 4, 8]
result = '\n'.join(str(num) for num in my_list)
print(result)
Output:
2
4
8
2. Using a For Loop
Alternatively you can use a for
loop to iterate over the items of the list and print each item on a separate line using print()
:
my_list = ['tutorial', 'reference', 'com']
for item in my_list:
print(item)
- The default behavior of the
print()
function is to add a newline at the end of each output.
Using os.linesep
for Platform-Specific Newlines
The os.linesep
attribute returns the correct newline character sequence for the current operating system, which is useful if you're developing cross-platform applications:
import os
my_str = f"tutorial{os.linesep}reference{os.linesep}com"
print(my_str)
Output:
tutorial
reference
com
Working with Newlines in Files
Newlines are added automatically when reading files with readlines()
, and can be added when writing to a file.
1. Reading Newlines from Files
When reading a file using readlines()
, newline characters are automatically added at the end of each line.
with open('example.txt', 'r', encoding="utf-8") as f:
lines = f.readlines()
print(lines)
for line in lines:
print(line, end='')
2. Writing Newlines to Files
Use \n
character when writing to a file to add newlines.
with open('example.txt', 'w', encoding="utf-8") as my_file:
my_file.write('tutorial' + '\n')
my_file.write('reference' + '\n')
my_file.write('com' + '\n')