Learn How to Convert a List to String in Python

Cover image for List to Strings in Python

In this post, we explore how to convert a list to string in Python! Python provides a .join function that makes this process incredibly easy and flexible. We’ll work through a number of examples and prepare some easy to use functions for you to take away and use in your own programming.

List to String Python with .join()

Let’s explore the join function a little bit. The list you pass into it can contain any of the following: strings, characters, and numbers. The function


Let’s begin by working through an example:

our_list = ['You', 'are', 'learning', 'with', 'datagy!']
joined = ' '.join(our_list)

This will return:

You are learning with datagy!

Are you enjoying our content? Consider following us on social media! Follow us on LinkedIn, Twitter, or Instagram!

Where you might run into issues

Earlier, we mentioned that you can convert lists of different data types. However, mixing data types within lists will result in issues. Let’s take a look at an example:

our_list = ['I','am', 'using', 'Python', 3]
joined = ' '.join(our_list)

This will return an error message, as shown below:

TypeError: sequence item 4: expected str instance, int found

We can easily fix this by changing all the types to strings:

our_list = ['I','am', 'using', 'Python', 3]
joined = ' '.join([str(item) for item in our_list])

This returns the text below:

I am using Python 3

In our join parameter, we used a list comprehension. We could have done this with a for loop as well, but Python list comprehensions make this process quite straightforward. If you want to learn more about list comprehensions in Python, check out our tutorial here.

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

Some Practical Examples to Turn a List to String in Python

An example of list to string in Python

Earlier, we promised you some functions to help you turn a list to strings in Python. In this section, we’ll provide some functions for you to try our and re-use in your own projects.

Free Download! All the Functions Below

To make your process a little easier, we’re providing a free download of all the functions below in a handy Python file. You can download it by clicking the button below:

Spaces Between Items

In the example below, we replicate our process above but turn the process into a function. You can then assign the resulting string to a variable as shown and print it out:

our_list = ['I','am', 'learning', 'Python', 'with', 'datagy!']

def spaces(your_list):
  return ' '.join(your_list)

space_string = spaces(our_list)

This returns:

I am learning Python with datagy!

Commas Between Items

If we wanted to delimit our items with commas, we could use the function below. Similar to our previous example, the result can be assigned to a variable:

our_list = ['I','am', 'learning', 'Python', 'with', 'datagy!']

def commas(your_list):
  return ','.join(your_list)

comma_string = commas(our_list)

This gives us the following output:


New Lines Between Items

In this following example, we’ll show how to separate our string with new lines for each item:

our_list = ['I','am', 'learning', 'Python', 'with', 'datagy!']

def new_lines(your_list):
  return '\n'.join(your_list)

new_lines_string = new_lines(our_list)

This gives us the following output:


Conclusion: List to String in Python

In this post, we learned how to turn lists into strings in Python. Remember to download your free Python file to get started with re-usable functions for your own projects.

Did we Mention our eBook?

Cover of Introduction to Python for Data Science

Want to learn Python for Data Science? Check out my ebook for as little as $10!