Home »
Python
Python - Convert List to String
Last Updated : May 01, 2025
In Python, you can convert a list to a string using methods like join()
. In this chapter, we will learn how to combine list elements into a single string, handling different types of data and separators with the help of examples.
Converting List to String Using join()
Method
You can convert a list to a string is by using the join()
method. The join()
method is a string method that joins all elements of a list into a single string, separated by a specified delimiter.
Example
In this example, we are converting a list of city names into a single string with spaces between the cities.
# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]
# Converting the list to a string
# with space as delimiter
cities_string = " ".join(cities)
# Displaying the converted string
print(cities_string)
The output of the above code would be:
New York London Paris Tokyo Sydney
Converting List to String Using join()
with Different Delimiters
You can use different delimiters to join the elements, such as commas, dashes, or line breaks.
Example
In this example, we are converting the list of cities into a string with a comma and space between each city.
# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]
# Converting the list to a string
# with comma and space as delimiter
cities_string = ", ".join(cities)
# Displaying the converted string
print(cities_string)
The output of the above code would be:
New York, London, Paris, Tokyo, Sydney
Converting a List of Non-String Elements to String
If your list contains non-string elements, you need to convert them to strings before using the join()
method.
Example
In this example, we are converting a list that contains both integers and strings into a single string.
# Creating a list with mixed data types
mixed_list = ["New York", 2022, "London", 2023, "Tokyo"]
# Converting all elements to strings
# before joining
mixed_list_string = " ".join(map(str, mixed_list))
# Displaying the converted string
print(mixed_list_string)
The output of the above code would be:
New York 2022 London 2023 Tokyo
Converting List to String Using a Loop
You can also loop through the list and concatenate the elements manually to create a string.
Example
In this example, we are using a loop to manually convert the list of cities into a string.
# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]
# Using a loop to convert the
# list to a string
cities_string = ""
for city in cities:
cities_string += city + " "
# Displaying the converted string
# Use strip() to remove the trailing space
print(cities_string.strip())
The output of the above code would be:
New York London Paris Tokyo Sydney
Converting List to String Using List Comprehension
List comprehension can also be used to create a more customized string representation of list elements, such as adding specific formatting.
Example
In this example, we are using list comprehension to convert the list of cities into a string, with each city enclosed in square brackets.
# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]
# Using list comprehension to
# add brackets around each city
cities_string = " ".join([f"[{city}]" for city in cities])
# Displaying the converted string
print(cities_string)
The output of the above code would be:
[New York] [London] [Paris] [Tokyo] [Sydney]
Exercise
Select the correct option to complete each statement about converting a list to a string in Python.
- Which method is used to convert a list of characters into a string?
- Given
my_list = ['H', 'e', 'l', 'l', 'o']
, what will ''.join(my_list)
return?
- If
my_list = [1, 2, 3, 4]
, what does ' '.join(map(str, my_list))
return?
Advertisement
Advertisement