Python List to String

Python Convert List to String

Dylan | Aug 03, 2020

Post Thumbnail

It is important to understand how to convert lists to strings in Python. We often store words or sentences together in lists to make it easy to loop through them and perform some process. But once we’ve finished processing, we normally want to convert them back into normal strings better suited for human readability.

Before we dive in, let’s look at an example list. Each item is an individual word in an interesting story.



cute_wabbit = ['A', 'little', 'girl', 'goes', 'into', 'a', 'pet', 'show', 'and', 'asks', 'for', 'a', 'wabbit.', 'The', 'shop', 'keeper', 'looks', 'down', 'at', 'her,', 'smiles', 'and', 'says:\n\n', '"Would', 'you', 'like', 'a', 'lovely', 'fluffy', 'little', 'white', 'rabbit,', 'or', 'a', 'cutesy', 'wootesly', 'little', 'brown', 'rabbit?\n\n"', '"Actually",', 'says', 'the', 'little', 'girl,', '"I', "don't", 'think', 'my', 'python', 'would', 'notice."']

Sorry you had to read that! Of course, we could just pass cute_wabbit to Python’s print() function and read the joke in the same format above but it would certainly hurt the delivery of the punchline. If you’re thinking there must be a better way, you’re right!

Python’s join()

By far, the simplest way to convert a list into a string in Python is with the built-in Python function str.join(). Join has a syntax that at first appears rather strange, but I promise there’s method to the madness! Take a look at the example below, using the cute_wabbit list defined above.



cute_wabbit_string = ' '.join(cute_wabbit)
print(cute_wabbit_string)

# Output
A little girl goes into a pet show and asks for a wabbit. The shop keeper looks down at her, smiles and says:

"Would you like a lovely fluffy little white rabbit, or a cutesy wootesly little brown rabbit?"

"Actually", says the little girl, "I don't think my python would notice."


Perhaps the funny story by Nick Leaton was enough to draw your attention away from the strange syntax. Why are we calling the method join on a string with a single space?

In this case, the single space is a delimiter. The character we would like to use to separate each of the items in our list as we combine them together. Take a look at what happens when we use different strings as delimiters below.



# Empty String
cute_wabbit_string = ''.join(cute_wabbit)
print(cute_wabbit_string)

# Empty String Output
Alittlegirlgoesintoapetshowandasksforawabbit.Theshopkeeperlooksdownather,smilesandsays:"Wouldyoulikealovelyfluffylittlewhiterabbit,oracutesywooteslylittlebrownrabbit?""Actually",saysthelittlegirl,"Idon'tthinkmypythonwouldnotice."

# Three Asterisks
cute_wabbit_string = ''.join(cute_wabbit)
print(cute_wabbit_string)

# Three Asterisks Output
A***little***girl***goes***into***a***pet***show***and***asks***for***a***wabbit.***The***shop***keeper***looks***down***at***her,***smiles***and***says:***"Would***you***like***a***lovely***fluffy***little***white***rabbit,***or***a***cutesy***wootesly***little***brown***rabbit?"***"Actually",***says***the***little***girl,***"I***don't***think***my***python***would***notice."


This gives us a lot of flexibility over how our lists are converted into strings! If this syntax still feels alien to you, I’ll try to break down what exactly is happening. In Python, everything is an object. In our case, the delimiter string is an object of the class str. This is the same class we access when we call str() for typecasting.

The method join is inside of this str class. It takes an iterable (something that can be looped through) as a parameter and according to the official Python docs, performs the following:

Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method.


Conclusion

There are other possible approaches to converting lists into strings in Python, such as using the map() and filter() functions; however, join() is the most common approach. As always, if you have any questions at all, please leave a comment below and I’ll do my best to answer it! Now get out there and experiment with converting your own lists into strings! Happy coding from Nimble Coding!

Comments