HOWTO · Python
How to Convert a List to String in Python
This tutorial demonstrates how to convert a list to string in Python using various methods, including the join() method, list comprehensions, the map() function, and traditional for loops. Learn effective techniques to simplify your coding tasks and enhance your programming skills. Whether you're working with strings or mixed data types, we've got you covered with clear examples and detailed explanations.
On this page
To turn a list into one string, call join() on the separator and pass it the list of strings. The separator is inserted between adjacent items; it is not added before the first item or after the last. For numbers or mixed values, convert each item to text first. Choose join() for the final assembly, then use a comprehension, map(), or a loop to prepare the items.
Convert a list of strings with join()
Run this example:
words = ["Hello", "World", "from", "Python"]
print("".join(words))
print(" ".join(words))
This shows two separator choices. An empty separator concatenates the strings directly; a space separator places one space between each pair. You can use any string as the separator, such as ", ", "-", or " | ". The items themselves are unchanged.
join() accepts an iterable whose elements are strings. It also works with tuples and other string iterables, but a list is common when the values are already collected. If an item is an integer or another non-string value, calling join() on the collection raises TypeError. Convert those values before joining rather than expecting join() to convert them implicitly.
Convert non-string items before joining
Use str() to get a string representation of each value, then join those strings:
values = [12, 7, 31]
result = ", ".join(str(value) for value in values)
print(result)
The expression str(value) for value in values is a generator expression, which supplies converted strings to join(). If you specifically need a list of converted items for later reuse, use a list comprehension:
values = [12, 7, 31]
strings = [str(value) for value in values]
result = ", ".join(strings)
print(result)
str() makes a human-readable representation; it is not a general-purpose serialization format for structured data. For example, converting a nested list or dictionary this way does not produce a reliable format for saving and reconstructing that data. Use a format designed for serialization when data must be stored or exchanged with its structure intact.
Choose between a list comprehension and map()
A comprehension makes the conversion or other per-item transformation visible beside the values it produces. It is a good fit when you need to change, filter, or explain the transformation in the same expression. The generator form in the previous section feeds join() without first building another list; use square brackets when a reusable list is useful.
map() applies a function to each item and returns an iterator. For a direct conversion with no other logic, it can be concise:
values = [12, 7, 31]
result = ", ".join(map(str, values))
print(result)
Both approaches convert each number with str() before join() assembles the result. Prefer the form that makes the transformation easiest for your readers to follow. Neither method changes the original list. For a custom conversion, replace str with a function that returns a string for every item.
Build a string with a for loop
A loop is useful when each item needs conditional handling or several steps before it is appended. For straightforward joining, use join(); repeatedly concatenating strings in a loop can do extra copying as the result grows. If you do need a loop, collect the pieces and join once at the end:
words = ["Python", "is", "useful"]
parts = []
for word in words:
parts.append(word)
result = " ".join(parts)
print(result)
The separator still controls the spacing, so this avoids adding and then trimming a trailing space. You can also add logic inside the loop, such as skipping selected items or formatting each one, as long as every collected piece is a string. For an integer list, append str(value) rather than the integer itself.
For the usual cases, call join() on the separator: use "".join(words) for direct concatenation or " ".join(words) for space-separated text. If the items are not already strings, convert them with a comprehension or map() first. Use a loop when it makes item-by-item logic clearer, and prefer joining once when assembling many pieces.