Python list to string
last modified November 16, 2026 Python 3.15
In this article we show how to convert a list to a string in Python.
The main tool for turning a list into a single string is the
join method of strings. It returns a string that is the
concatenation of the strings in the given iterable, with the string on which
it is called placed between them. Since join accepts only
strings, other values are first converted with str, typically via
map or a generator expression.
Joining a list of strings
The separator is the string on which join is called; the list is
its argument.
words = ['a', 'visit', 'to', 'London']
slug = '-'.join(words)
print(slug)
print(' '.join(words))
print(''.join(['p', 'y', 't', 'h', 'o', 'n']))
print(repr(', '.join([])))
We create a URL slug by joining words with a hyphen, then a sentence by joining them with a space. An empty separator concatenates the strings directly. Joining an empty list produces an empty string.
$ python join_words.py a-visit-to-London a visit to London python ''
Lists with non-string elements
The join method does not convert its elements. If the list
contains a number or another non-string value, it raises a
TypeError.
words = ['There', 'are', 3, 'chairs', 'in', 'the', 'room']
try:
msg = ' '.join(words)
except TypeError as e:
print(f'error: {e}')
The error message tells us the position and the type of the offending element.
$ python join_error.py error: sequence item 2: expected str instance, int found
Converting elements with a generator expression
To join a list of mixed values, we convert each element to a string first. A
generator expression passed to join does this without building an
intermediate list.
words = ['There', 'are', 3, 'chairs', 'and', 2, 'lamps', 'in',
'the', 'room']
msg = ' '.join(str(word) for word in words)
print(msg)
The str(word) call converts the integers to strings; strings are
returned unchanged.
$ python join_generator.py There are 3 chairs and 2 lamps in the room
Converting elements with map
The map function returns an iterator that applies the given
function to every item of an iterable. Combined with str, it is
the most compact way to join arbitrary values.
words = ['There', 'are', 3, 'chairs', 'and', 2, 'lamps', 'in',
'the', 'room']
msg = ' '.join(map(str, words))
print(msg)
vals = [1, 2, 3, 4, 5]
print('+'.join(map(str, vals)), '=', sum(vals))
We apply str to each element with map and join the
results. In the second part, we build an addition expression from a list of
integers.
$ python join_map.py There are 3 chairs and 2 lamps in the room 1+2+3+4+5 = 15
Formatting elements
When the elements need a specific format, we use an f-string inside the generator expression.
prices = [12.5, 3, 7.249, 102]
print(', '.join(f'{p:.2f}' for p in prices))
print(' | '.join(f'{p:>7.2f}' for p in prices))
rows = [['Paul', 34], ['Lucy', 28], ['Robert', 51]]
text = '\n'.join(f'{name};{age}' for name, age in rows)
print(text)
The first line prints the prices with two decimal places; the second one also right-aligns them in columns seven characters wide. Finally, we turn a list of rows into semicolon-separated lines by joining them with the newline character.
$ python join_format.py 12.50, 3.00, 7.25, 102.00 12.50 | 3.00 | 7.25 | 102.00 Paul;34 Lucy;28 Robert;51
The str and repr of a list
Calling str on a list, or putting it into an f-string, produces
the list's representation with square brackets, commas, and quoted strings.
This is useful for debugging but rarely what we want to show to users.
words = ['sky', 'cup', 'forest']
vals = [1, 2.5, None, 'text']
print(str(words))
print(f'{vals}')
The elements are shown with their repr: strings in quotes and
None as a keyword. To get just the values, use join.
$ python str_repr.py ['sky', 'cup', 'forest'] [1, 2.5, None, 'text']
Printing a list with unpacking
When we only need to print the elements, we can unpack the list into the
arguments of the print function. The sep parameter
sets the separator, and print converts the values to strings
itself.
words = ['sky', 'cup', 'forest'] print(*words) print(*words, sep=', ') print(*words, sep='\n')
The *words expression passes each element as a separate
argument. By default, print separates them with a space.
$ python print_unpack.py sky cup forest sky, cup, forest sky cup forest
Human-readable enumeration
In text meant for people, the last two items of an enumeration are usually
joined with a word such as and. A small function combines
join with an f-string to produce such output.
def human_join(items, conj='and'):
items = [str(e) for e in items]
if len(items) <= 2:
return f' {conj} '.join(items)
return f'{", ".join(items[:-1])}, {conj} {items[-1]}'
print(human_join(['apples']))
print(human_join(['apples', 'pears']))
print(human_join(['apples', 'pears', 'plums', 'figs']))
print(human_join([1, 2, 3], conj='or'))
Lists with one or two items are joined with the conjunction alone. Longer lists have all items but the last joined with commas, followed by the conjunction and the last item. The elements are converted to strings first, so the function also works for numbers.
$ python natural_list.py apples apples and pears apples, pears, plums, and figs 1, 2, or 3
Concatenating in a loop
A string can also be built with the += operator in a loop. This
approach is shown for comparison; it is longer and less efficient than
join.
words = ['There', 'are', 3, 'chairs', 'and', 2, 'lamps', 'in',
'the', 'room']
msg = ''
for word in words:
msg += f'{word} '
print(repr(msg))
print(repr(msg.rstrip()))
Strings are immutable, so repeated += may copy the growing
string over and over, and the loop leaves a trailing space that has to be
stripped with rstrip. The join method computes the
final size once and places separators only between the elements.
$ python concat_loop.py 'There are 3 chairs and 2 lamps in the room ' 'There are 3 chairs and 2 lamps in the room'
Converting a list to JSON
To turn a list into a string that other programs can parse, we serialize it
to JSON with the json.dumps function.
import json data = ['sky', 3, 2.5, True, None, 'žltý'] print(json.dumps(data)) print(json.dumps(data, ensure_ascii=False))
JSON uses double quotes, true, and null. By default,
non-ASCII characters are escaped; with ensure_ascii=False, they
are kept as they are. The json.loads function converts the string
back to a list.
$ python to_json.py ["sky", 3, 2.5, true, null, "\u017elt\u00fd"] ["sky", 3, 2.5, true, null, "žltý"]
Source
str.join – Python documentation
map – Python built-in functions
In this article we have converted lists to strings in Python. See also Python split string, Python f-string, and Python map built-in function.
Author
List all Python tutorials.