Data takes many shapes and forms - one of the most common data structure being lists/arrays. Strings are, essentially, sequences of alphanumeric characters. The parallel between these is apparent, and we oftentimes convert between one and the other.
In this guide, we'll take a look at how to convert/collect a list to a string in Python.
Convert List to String in Python with join()
The general approach you'll be using is the
join() function, with a couple of options to choose from. The function is invoked on a string denoting a separator - and this separator will be added between all joined items.
Note: Although you can define a separator upfront, it's typically just instantiated as an empty string, upon which the
join() function is called.
Let's take a look at a list of characters that we'd like to join:
base_list = ['M', 'K', 'V', 'N', 'L', 'I', 'L', 'F', 'S', 'L', 'L', 'V']
The simplest way to join these into a single string is to just pass the list into the
string = "".join(base_list) print(string)
This results in:
We've joined a few characters, denoting the amino-acids present at the start of the sequence of a protein found in the E. coli bacteria!
Each of these elements is of type
str, so there's no issue in naturally putting these into a string. Though, sometimes, you might want to convert a certain element into a different type.
Additionally, if you use a different delimiter, you can get a different representation as well:
string = ", ".join(base_list)
This results in:
M, K, V, N, L, I, L, F, S, L, L, V
Convert List to String in Python with List Comprehension
Note: Python implicitly converts any data types it can, so you won't need to explicitly convert them in most cases.
If you do have to convert the elements, you can simply use a list comprehension:
string = "".join(str(element) for element in base_list)
Each element in the list is now passed through the
str() function, which converts them to a string. You can substitute this with any other function, including custom ones. For instancec, let's create a function that capitalizes each element in the 'off chance' an element isn't capitalized as it should be:
base_list = ['m', 'K', 'V', 'N', 'L', 'I', 'L', 'f', 'S', 'L', 'L', 'V'] def capitalize_char(char): return char.upper() string = "".join(capitalize_char(element) for element in base_list) print(string)
f have been flipped to lowercase, which shouldn't have happened. We've passed in the
capitalize_char() function into the list comprehension, returning the capitalized version of each element we've passed in. This results in:
Convert List to String in Python with map()
Finally, for a more clean and easy way to pass elements through a function - we can use
map(). Generally, the
map() function is used to map values of one collection to another, typically using a different (or even reduced) format.
For instance, you could map each character to another list of booleans, where each boolean represents if the character is uppercase or not.
Of course, you can also always do a 1-to-1 mapping using the
map() function and, well, just map a string to a string, joining them together into a single one:
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
base_list = ['M', 'K', 'V', 'N', 'L', 'I', 'L', 'F', 'S', 'L', 'L', 'V'] string = "".join(map(str, base_list)) print(string)
This results in:
If you'd like to read more about mapping in Python, read our Guide to map(), filter() and reduce() in Python!
In this short guide, we've taken a look at how to convert a list to a string in Python, collecting the elements into a single piece.
Strings are inherently lists of characters, and they behave in much the same way, but can be treated as individual elements as well.
You've learned how to use the
join() function in its plain form, as well as how to use it with list comprehensions, custom methods and the