Last updated: Apr 9, 2024
Reading timeยท4 min
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.
variable = "bobby" my_str = variable + '\n' + 'hadz' # bobby # hadz print(my_str)
The example uses the addition (+) operator to print a new line after a variable.
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.
variable = "bobby" my_str = f'{variable}\nhadz' # bobby # hadz print(my_str)
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.
Alternatively, you can print new lines by using a multiline string.
variable = "bobby" my_str = f"""\ {variable} 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:
End of lines are automatically included in triple-quoted strings, so we don't have to add a newline character at the end.
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.
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.
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()
.
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.
for
loopAlternatively, you can use a for loop to iterate over the list and print each item.
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()
.
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.
os.linesep
instead of \n
to print newlines in PythonAn 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.
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 as follows.
bobby hadz com
You can learn more about the related topics by checking out the following tutorials: