Last updated: Apr 8, 2024
Reading time·3 min
To join a list with a newline character in Python:
join()
method on a string containing a newline char ('\n'
).join
method.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.
You can print the result with the repr() function to see the newline characters in the string.
my_list = ['bobby', 'hadz', 'com'] result = '\n'.join(my_list) print(repr(result)) # 👉️ 'bobby\nhadz\ncom'
Note that the method raises a TypeError
if there are any non-string values in
the iterable.
If your list contains numbers or other types, convert all of the values to
string before calling join()
.
my_list = ['bobby', 'hadz', 'com', 4, 5] result = '\n'.join(map(str, my_list)) # ️bobby # hadz # com # 4 # 5 print(result)
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
str.join()
method.The string the join()
method is called on is used as the separator between
elements.
my_list = ['bobby', 'hadz', 'com'] result = '-'.join(my_list) # 👇️bobby # hadz # com print(result) # 👉️ 'bobby-hadz-com'
To join the list items into a string with a newline separator, call the join
method on a string that contains a newline character ('\n'
).
my_list = ['bobby', 'hadz', 'com'] result = '\n'.join(my_list) # 👇️bobby # hadz # com print(result)
You can use the addition operator if you need to join two strings with a new line between them.
string1 = 'bobby' string2 = 'hadz' # ✅ using addition (+) operator result = string1 + '\n' + string2 # bobby # hadz print(result) # ------------------------------------- # ✅ using str.join() a_list = ['bobby', 'hadz', '.com'] result = '\n'.join(a_list) # bobby # hadz # .com print(result)
The newline (\n
) character can be used to add a new line in Python.
The first example uses the addition (+) operator to join the strings with a newline character in between.
string1 = 'bobby' string2 = 'hadz' result = string1 + '\n' + string2 # bobby # hadz print(result)
When used with multiple strings, the addition (+) operator concatenates them.
print('a' + 'b' + 'c') # 👉️ 'abc'
You can also use a formatted string literal to achieve the same result.
string1 = 'bobby' string2 = 'hadz' result = f'{string1}\n{string2}\n.com' # bobby # hadz # .com print(result)
Formatted string literals
(f-strings) let us include expressions inside of a string by prefixing the
string with f
.
var1 = 'bobby' var2 = 'hadz' result = f'{var1}{var2}' print(result) # 👉️ bobbyhadz
Make sure to wrap expressions in curly braces - {expression}
.
An alternative approach is to add the strings to an iterable, such as a list, an join them with a newline character separator.
a_list = ['bobby', 'hadz', '.com'] result = '\n'.join(a_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.
The string the method is called on is used as the separator between the elements.
You can learn more about the related topics by checking out the following tutorials: