Convert a list to a string in Python

Copied to clipboard.
Series: Strings
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read Watch as video Python 3.8—3.12
Python Morsels
Watch as video

Let's talk about how to turn a list into a string in Python.

Using str to convert a list to a string

Let's say we have a list of strings called things:

>>> things = ["apples", "ducks", "lemons", "cats", "potatoes"]

If we'd like to turn this list into a single string, we could pass it to the built-in str function:

>>> str(things)
"['apples', 'ducks', 'lemons', 'cats', 'potatoes']"

But the output we get probably isn't what we were looking for. At least not if we're trying to pass this to an end-user rather than another programmer.

So we need to ask ourselves, what are we really trying to do here? Basically, what I'd like to do is join together each of the strings in this list by some kind of delimiter (like a space for example).

Converting a list to a string with join

Many programming languages have a join method that you can call on the list type or the array type, passing it a delimiter to join together all the strings in that list by a given delimiter. Python doesn't have this!

>>> things.join(" ")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'join'

In Python, our list type does not have a join method. Instead, our string type has a join method:

>>> " ".join(things)
'apples ducks lemons cats potatoes'

Here, we've asked the space character (" ") to kindly use itself as a delimiter, joining together the items in the list of strings we've passed to it.

Using a delimiter while joining

We can use any string as a delimiter. For example, using ", " as a delimiter would put a comma and a space between each of these strings:

>>> ", ".join(things)
'apples, ducks, lemons, cats, potatoes'

Or an empty string ("") would put nothing between each of them, concatenating them all (smooshing them together into one long word):

>>> "".join(things)

The string join method isn't only for lists

Why does Python do it this way? Why is the join method on strings instead of the lists? This seems kind of backwards, right?

Python does it this way for the sake of flexibility.

In Python, we have lots of types of iterables (not just lists) and we tend to think in terms of duck typing (if it looks like a duck and quacks like a duck, it's a duck). That is, we care about the behavior (e.g. iterability) of an object instead of its type (e.g. list).

Because the join method is on the string type, we can pass in any iterable of strings to it. For example we can join together a tuple of strings:

>>> words = ("apple", "animal", "Australia")
>>> " ".join(words)
'apple animal Australia'

Or we can join together a file object (file objects are iterable in Python and when you loop over them you get lines):

>>> my_file = open("things.txt")
>>> "".join(my_file)

We can also join together a generator object:

>>> my_file = open("things.txt")
>>> ", ".join(line.rstrip("\n") for line in my_file)
'apples, ducks, lemons, cats, potatoes'

That generator expression removes new lines from the end of each line in a file and we're using the string join method to join those lines together with a comma and a space.

Converting an iterable of non-strings into one string

What if the iterable that we're joining together isn't an iterable of strings? What if it's an iterable of numbers?

When we try to join together an iterable of items that aren't strings we'll get a TypeError:

>>> numbers = [2, 1, 3, 4, 7, 11, 18]
>>> " ".join(numbers)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int found

Because we know that the join method accepts any iterable of strings, we could write a generator expression that converts each of our items into a string by passing it to the built-in str function:

>>> " ".join(str(n) for n in numbers)
'2 1 3 4 7 11 18'

So even if you're working with an iterable of non-strings, you can join them together as long as you do a little bit of pre-processing first.

Use the string join method to turn a list into a string in Python

Need to convert a list to a string in Python?

Just make a string for your delimiter and then call the join method on it:

>>> ", ".join(my_list_of_strings)

This works on any iterable-of-strings, not just lists of strings.

Series: Strings

Regardless of what you're doing in Python, you almost certainly use strings all the time. A string is usually the default tool we reach for when we don't have a more specific way to represent our data.

To track your progress on this Python Morsels topic trail, sign in or sign up.

Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.

Python Morsels
Watch as video