How to Convert a List to a String in Python?

In this tutorial, I will explain how to convert a list to a string in Python. As a Python developer working on a project, I came across a scenario where I needed to convert a list to a string. This made me want to explore this topic. I will share my findings with examples and screenshots.

Convert a List to a String in Python

Before getting into the methods for converting a list to a string, let’s briefly review what lists and strings are in Python.

A list is a collection of items enclosed in square brackets [] separated by commas. Lists are mutable, meaning you can modify, add, or remove elements after creation. Here’s an example of a list:

cities = ["New York", "Los Angeles", "Chicago", "Houston"]

On the other hand, a string is a sequence of characters enclosed in single quotes '' or double quotes "". Strings are immutable, which means you cannot change their contents once created. Here’s an example of a string:

message = "Hello, World!"

Read How to Remove the Last Element from a List in Python?

1. Use join() method

The most common and efficient way to convert a list to a string in Python is by using the join() method. The join() method is a string method that concatenates the elements of an iterable (such as a list) into a single string.

Here’s the general syntax of the join() method:

'separator'.join(iterable)
  • 'separator' is the string that will be used as a separator between the elements.
  • iterable is the list or any iterable object that you want to convert to a string.

Let’s take an example to understand how the join() method works:

names = ["John", "Alice", "Bob", "Emma"]
result = ', '.join(names)
print(result)

Output:

John, Alice, Bob, Emma

You can see the output in the screenshot below.

Convert a List to a String in Python

In this example, we have a list of names called names. We use the join() method with ', ' as the separator to join the elements of the names list into a single string. The resulting string result contains the names separated by commas and spaces.

Check out How to Flatten a List of Lists in Python?

2. Use List Comprehension

Another way to convert a list to a string is by using Python list comprehension. List comprehension allows you to create a new list based on an existing list.

Here’s an example that demonstrates converting a list to a string using list comprehension:

states = ["California", "Texas", "Florida", "New York"]
result = ''.join([state[0] for state in states])
print(result)

Output:

CTFN

You can see the output in the screenshot below.

How to Convert a List to a String in Python

In this example, we have a list of U.S. states called states. We use list comprehension [state[0] for state in states] to create a new list containing the first character of each state. Then, we use the join() method with an empty string '' as the separator to concatenate the characters into a single string.

Read How to Randomly Select from a List in Python?

3. Use map() Function

The map() function in Python applies a given function to each item of an iterable and returns a new iterable with the results. You can use map() along with the join() method to convert a list to a string.

Here’s an example that demonstrates converting a list to a string using map():

cities = ["Chicago", "Houston", "Philadelphia", "Phoenix"]
result = ', '.join(map(str, cities))
print(result)

Output:

Chicago, Houston, Philadelphia, Phoenix

You can see the output in the screenshot below.

Convert a List to a String in Python map() function

In this example, we have a list of U.S. cities called cities. We use the map() function with the str function to convert each element of the cities list to a string. Then, we use the join() method with ', ' as the separator to join the string representations of the cities into a single string.

Read How to Remove Duplicates from a List in Python?

Example: Generate a Comma-Separated String from a List

Let’s consider a real-life scenario where you have a list of customer names, and you need to generate a comma-separated string of their names for a report.

customers = ["John Smith", "Emma Johnson", "Michael Davis", "Olivia Wilson"]
report = "Customers: " + ', '.join(customers)
print(report)

Output:

Customers: John Smith, Emma Johnson, Michael Davis, Olivia Wilson

In this scenario, we have a list of customer names called customers. We use the join() method with ', ' as the separator to join the names into a comma-separated string. We then concatenate the string “Customers: ” with the comma-separated names to create the final report string.

Check out How to Find Duplicates in a Python List?

Convert a List of Integers to a String

If you have a list of integers and want to convert it to a string, you need to first convert each integer to a string before joining them. You can achieve this by using a list comprehension or the map() function.

Here’s an example using a list comprehension:

numbers = [1, 2, 3, 4, 5]
result = ''.join(str(num) for num in numbers)
print(result)

Output:

12345

In this example, we have a list of integers called numbers. We use a list comprehension str(num) for num in numbers to convert each integer to a string. Then, we use the join() method with an empty string '' as the separator to concatenate the string representations of the integers into a single string.

Read How to Create an Empty List in Python?

Customize the Separator when Converting a List to a String

By default, the join() method uses an empty string as the separator when joining the elements of a list. However, you can specify any custom separator according to your needs.

Here’s an example that demonstrates using a custom separator:

fruits = ["apple", "banana", "orange", "grape"]
result = ' | '.join(fruits)
print(result)

Output:

apple | banana | orange | grape

In this example, we have a list of fruits called fruits. We use the join() method with ' | ' as the separator to join the elements of the fruits list. The resulting string result contains the fruits separated by vertical bars (|) and spaces.

Check out How to Remove All Instances of an Element from a List in Python?

Conclusion

In this tutorial, I helped you to learn how to convert a list to a string in Python. I explained the join() method, list comprehension and the map() function to achieve this task. I also discussed how to customize the separator and handle different data types, such as integers, along with a real-world example.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.