Skip to content

Learn How to Convert a List to String in Python

Learn How to Convert a List to String in Python Cover Image

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

seperator.join(list_or_iterable)

Let’s begin by working through an example:

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

This will return:

You are learning with datagy!

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)
print(joined)

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])
print(joined)

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.

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.

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)
print(space_string)

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)
print(comma_string)

This gives us the following output:

I,am,learning,Python,with,datagy!

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)
print(new_lines_string)

This gives us the following output:

I
am
learning
Python
with
datagy!

Conclusion: List to String in Python

In this post, we learned how to turn lists into strings in Python. Being able to work with lists in different ways is an important skill, especially when reading data from files or the web. The Python str.join() function makes this very easy to do.

Additional Resources

To learn more about related topics, check out the tutorials below:

Leave a Reply

Your email address will not be published.