To convert a list
lst of strings to a string, use the
''.join(lst) method with an empty separator string between the elements. If you have a list of objects, first convert each element to a string and join the result with the generator expression
''.join(str(x) for x in lst).
Problem: Given a list of elements. How to convert them to a string?
Example: Given the following list of strings.
lst = ['a', 'b', 'c']
You want to convert the list of strings to the following string:
What’s the most Pythonic way of converting a list to a string? The answer depends on the nuances.
Here’s a quick overview before we dive into each of the methods:
Exercise: Run the code! What’s the most Pythonic way in your opinion?
Method 1: Join List of Strings
The most straightforward way is to use the
string.join(iterable) method that concatenates all values in the
iterable (such as a list) using the separator
string in between the elements.
lst = ['a', 'b', 'c'] s = ''.join(lst)
The output is the following string:
print(s) # abc
Due to its conciseness and efficiency, this is the most Pythonic way of converting a list of strings to a string. However, the join() method expects that you pass a list of strings. If you have a list of non-strings, it will throw an error!
Related article: The Ultimate Guide to Python Join
Method 2: Join List of Non-Strings with Generator Expression
So, what to do if you want to convert a list of general objects to a string?
The most Pythonic way to concatenate a list of objects is the expression
''.join(str(x) for x in lst) that converts each object to a string using the built-in
str(...) function in a generator expression. You can concatenate the resulting list of strings using the
join() method on the empty string as a delimiter. The result is a single string value that’s the concatenation of the objects’ string representations.
lst = [1, 2, 3] s = ''.join(str(x) for x in lst) print(s) # 123
This general method works for all objects (because all objects implement the
__str__ method per default). It’s the most Pythonic way of converting a list of non-string
Related article: What’s the most Pythonic way to join a list of objects?
Method 3: String Concatenation with +
Just for the sake of completeness, I want to highlight that you can also concatenate all strings in a list by using the + operator. If you have a list with a few objects, this is a viable option:
lst = ['a', 'b', 'c'] s = lst + lst + lst print(s) # abc
This is inefficient because each
+ operator creates a new string. For
n list elements, you create
n-1 new strings in memory.
Related article: The
+ operator for string concatenation.
This must be slightly modified when concatenating a list of objects to a single string:
Method 4: String Concatenation with + and str()
Again, if you have a list of objects, you need to convert each object to a string first:
# Method 4: String Concatenation with + and str() lst = [1.0, 2.0, 3.0] s = str(lst) + str(lst) + str(lst) print(s) # 1.02.03.0
This is very tedious and it deserves to be titled the “least Pythonic way to convert a list to a string”. You should prefer the general Method 2 that’s not only shorter and more efficient, but also more readable and generally applicable.
Method 5: Use Map + Join
The map function allows you to convert each element to a string first. You can then join the strings using the standard string.join() method on the resulting iterable of strings.
lst = [1, 2, 'hello', 3.2] s = ''.join(map(str, lst)) print(s) # 12hello3.2
This works beautifully for list of objects as well and it’s quite Pythonic. Many Python coders like this functional style—but I don’t consider it the most Pythonic one. Python code should be readable. Guido van Rossum, Python’s creator, tried to avoid functional programming because he didn’t find it readable compared to list comprehension or its generalization “generator expressions” (see Method 2).
Related Article: The map() function in Python
Method 6: Simple Loop
Let’s see what coders who come from another programming language such as Java would do:
lst = [1, 2, 'hello', 3.2] s = '' for x in lst: s += str(x) print(s) # 12hello3.2
They’d first create an empty string. Then, they’d add the string representation of each list element to the string until all list elements are added.
This is highly inefficient because of the repeated creation of new strings and it needs three lines instead of one. As it’s often the case in Python, you can avoid loops by using Python’s powerful built-in capabilities.
Related article: Go vs Python
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.