Print a String and a List in one line in Python

avatar

Borislav Hadzhiev

Last updated: Sep 3, 2022

banner

Photo from Unsplash

Print a String and a List in one line in Python #

Use a formatted string literal to print a string and a list in one line, e.g. print(f'{my_str}{my_list}'). Formatted string literals let us include expressions and variables inside of a string by prefixing the string with f.

main.py
my_str = 'The numbers are: ' my_list = [4, 8, 2] # ✅ print string and list in one line (f-string) result = f'{my_str}{my_list}' print(result) # 👉️ The numbers are: [4, 8, 2] # --------------------------------------------- # ✅ print string and list in one line (comma-separated args) print(my_str, my_list, sep='') # 👉️ The numbers are: [4, 8, 2] # --------------------------------------------- # ✅ print string and list in one line (unpacking) print('The numbers are:', *my_list) # 👉️ The numbers are: 4 8 2 # --------------------------------------------- # ✅ print string and list in one line (str.join()) result = my_str + ', '.join(str(item) for item in my_list) print(result) # 👉️ The numbers are: 4, 8, 2

The first example uses a formatted string literal to print a string and a list in one line.

main.py
my_str = 'The numbers are: ' my_list = [4, 8, 2] result = f'{my_str}{my_list}' print(result) # 👉️ The numbers are: [4, 8, 2]
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
my_list = [4, 8, 2] result = f'The numbers are: {my_list}' print(result) # 👉️ The numbers are: [4, 8, 2]

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

Alternatively, you can call the print() function with multiple, comma-separated arguments.

Print a String and a List in one line using print() with commas #

Pass multiple, comma-separated arguments to the print() function to print a string and a list in one line. The print() function will print the values next to one another separated by spaces.

main.py
my_str = 'The numbers are:' my_list = [4, 8, 2] print(my_str, my_list) # 👉️ The numbers are: [4, 8, 2]

We passed multiple, comma-separated arguments to the print() function to print a string and a list on the same line.

You can use the sep keyword argument if you need to adjust the separator between the values.
main.py
print('abc', [1, 2, 3], sep='') # 👉️ abc[1, 2, 3] print('abc', [1, 2, 3]) # 👉️ abc [1, 2, 3]

By default, the sep argument is set to a space.

main.py
print('a', 'b', 'c', sep="_") # 👉️ "a_b_c" print('a', 'b', 'c', sep="_", end="!!!") # 👉️ "a_b_c!!!"

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

The end argument is set to a newline character (\n) by default.

You can also use the iterable unpacking operator to print a string and a list in one line.
main.py
my_list = [4, 8, 2] print('The numbers are:', *my_list) # 👉️ The numbers are: 4 8 2

The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.

main.py
print(*my_list) # 👉️ 4 8 2 print(*my_list, sep=', ') # 👉️ 4, 8, 2

If you need to adjust the separator between the elements, use the sep keyword argument.

You can also use the str.join() method to achieve the same result.

main.py
my_str = 'The numbers are: ' my_list = [4, 8, 2] result = my_str + ', '.join(str(item) for item in my_list) print(result) # 👉️ The numbers are: 4, 8, 2

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.

If your iterable contains numbers or other types, convert all of the values to strings before calling join().

If your list doesn't contain numbers, pass it directly in the call to the str.join() method.

main.py
my_str = 'The letters are: ' my_list = ['a', 'b', 'c'] result = my_str + ', '.join(my_list) print(result) # 👉️ The letters are: a, b, c

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

main.py
my_str = 'The letters are: ' my_list = ['a', 'b', 'c'] result = my_str + '-'.join(my_list) print(result) # 👉️ The letters are: a-b-c

If you don't need a separator and just want to join the iterable's elements into a string, call the join() method on an empty string.

main.py
my_str = 'The letters are: ' my_list = ['a', 'b', 'c'] result = my_str + ''.join(my_list) print(result) # 👉️ The letters are: abc
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.