Join a List with a Newline character in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Table of Contents

  1. Join a list with a newline character in Python
  2. Join two strings with a new line between them in Python

# Join a list with a newline character in Python

To join a list with a newline character in Python:

  1. Call the join() method on a string containing a newline char ('\n').
  2. Pass the list to the join method.
  3. The result will be a string containing the list items separated by a newline character.
main.py
my_list = ['bobby', 'hadz', 'com'] result = '\n'.join(my_list) # ️bobby # hadz # com print(result)

join list with newline character

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.

You can print the result with the repr() function to see the newline characters in the string.

main.py
my_list = ['bobby', 'hadz', 'com'] result = '\n'.join(my_list) print(repr(result)) # 👉️ 'bobby\nhadz\ncom'

# Handling non-string values in the list

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().

main.py
my_list = ['bobby', 'hadz', 'com', 4, 5] result = '\n'.join(map(str, my_list)) # ️bobby # hadz # com # 4 # 5 print(result)

handling non string values in list

The code for this article is available on GitHub

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

We used the function to convert each item in the list to a string, before passing the items to the str.join() method.

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

main.py
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').

main.py
my_list = ['bobby', 'hadz', 'com'] result = '\n'.join(my_list) # 👇️bobby # hadz # com print(result)

# Join two strings with a new line between them in Python

You can use the addition operator if you need to join two strings with a new line between them.

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

join two strings with new line between them

The code for this article is available on GitHub

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.

main.py
string1 = 'bobby' string2 = 'hadz' result = string1 + '\n' + string2 # bobby # hadz print(result)

When used with multiple strings, the addition (+) operator concatenates them.

main.py
print('a' + 'b' + 'c') # 👉️ 'abc'

# Join two strings with a new line between them using an f-string

You can also use a formatted string literal to achieve the same result.

main.py
string1 = 'bobby' string2 = 'hadz' result = f'{string1}\n{string2}\n.com' # bobby # hadz # .com print(result)

join two strings with newline 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.

main.py
var1 = 'bobby' var2 = 'hadz' result = f'{var1}{var2}' print(result) # 👉️ bobbyhadz

Make sure to wrap expressions in curly braces - {expression}.

# Join two strings with a new line between them using a list

An alternative approach is to add the strings to an iterable, such as a list, an join them with a newline character separator.

main.py
a_list = ['bobby', 'hadz', '.com'] result = '\n'.join(a_list) # bobby # hadz # .com print(result)

join two strings with new line using list

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.

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

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