Python: Convert List to String with join()

Python: Convert List to String with join()

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 join() function:

string = "".join(base_list)
print(string)

This results in:

MKVNLILFSLLV

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)

m and 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:

MKVNLILFSLLV

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.

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!

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:

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:

MKVNLILFSLLV

If you'd like to read more about mapping in Python, read our Guide to map(), filter() and reduce() in Python!

Conclusion

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 map() function!

Last Updated: November 23rd, 2021
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

David LandupAuthor

Entrepreneur, Software and Machine Learning Engineer, with a deep fascination towards the application of Computation and Deep Learning in Life Sciences (Bioinformatics, Drug Discovery, Genomics), Neuroscience (Computational Neuroscience), robotics and BCIs.

Great passion for accessible education and promotion of reason, science, humanism, and progress.

Free
Course

Graphs in Python - Theory and Implementation

# python# data structures# algorithms# computer science

Graphs are an extremely versatile data structure. More so than most people realize! Graphs can be used to model practically anything, given their nature of...

David Landup
Dimitrije Stamenic
Jovana Ninkovic
Details
Project

Data Visualization in Python: The Collatz Conjecture

# python# matplotlib# data visualization

The Collatz Conjecture is a notorious conjecture in mathematics. A conjecture is a conclusion based on existing evidence - however, a conjecture cannot be proven....

David Landup
Jovana Ninkovic
Details

© 2013-2022 Stack Abuse. All rights reserved.

DisclosurePrivacyTerms