Suppose you are building an application that shows the grades a student has earned on a test. These grades are stored in a list but you want to present them as a string. How would you go about doing that? That’s where the Python join() method can be useful.
In this guide, we’re going to talk about how to convert a Python list to a string using the join() method. We will discuss how to join a list of strings and a list containing other data types into a single string. Let’s get started.
Python Convert List to String
You can use the Python join() method to convert a list into a string. The join() method reads all the items in an object (i.e. a list or a tuple) and merges them together. If you have a list of strings, you only need the join() method. You will need to use other methods in addition to the join() method if you want to change a list that contains non-string data types into a string.
Python Convert List to String: List of Strings
The syntax to convert a list of strings into a single string is:
values = ["value1", "value2"] new_values = ", ".join(values) print(new_values)
This code merges our two values into a string. The value inside our quotation marks before the .join() method tells us how each string will be separated. In this case, we separated each string with a comma and then a space. Our code returns:
value1, value2
Suppose we have a list that contains the ingredients necessary to bake a scone. We have these ingredients stored as a list but we want to convert the list to a string. Our intention is to display a message on the Python console stating what ingredients are needed for a recipe.
To start, let’s define our list of ingredients:
ingredients = ["350g self-raising flour", "85g butter", "3 tablespoons caster sugar", "175ml milk", "1 teaspoon baking powder", "1 teaspoon vanilla extract"]
Next, let’s use the join() method to convert our list into a single string:
print(", ".join(ingredients))
We use the .join() method to combine our list. Each item in our list will appear between a “, ” string in our final string. This will make it easier for us to read the ingredients. Let’s run our code and see what happens:
350g self-raising flour, 85g butter, 3 tablespoons caster sugar, 175ml milk, 1 teaspoon baking powder, 1 teaspoon vanilla extract
Our code successfully displays the list of ingredients.
Python Convert List to String: Integers or Floats, or a Combination
The join() method only combines lists of strings. To convert a list that contains at least one integer or float to a string, you will need to use some method of converting the integers and floats to strings. Common methods include list comprehensions and the map() method.
Suppose we have a list that contains all of the ratings a scone has received by a panel of judges. Our list contains only integers:
ratings = [4, 3, 4, 5, 4]
We want to convert this list into a string. To do so, we could use this code:
final_ratings = ", ".join([str(r) for r in ratings])
We use a list comprehension to iterate over every item in the ratings list. We convert each item to a string using the str() method. Then, we use the join() method to add all of these values into the final_ratings string. Let’s display the contents of our string to the console:
print("The judges scored this recipe: " + final_ratings)
Our code returns:
The judges scored this recipe: 4, 3, 4, 5, 4
We have successfully printed our list of integers to the console.
We could also use the map() method to achieve the same result as our example above:
ratings = [4, 3, 4, 5, 4] final_ratings = ", ".join(map(str, ratings)) print("The judges scored this recipe: " + final_ratings)
The map() method converts every item in our ratings list to a string. We pass the result of the map() method through the join() method to create a string.
Our code returns:
The judges scored this recipe: 4, 3, 4, 5, 4
As you can see, our code returns the same result as our example that used a list comprehension instead of the map() method.
Conclusion
The Python join() method lets you combine a list of strings into a string. You can use a list comprehension or the map() function in addition to the join() method to convert a list that contains at least one integer or float into a string.
Do you want to learn more about Python? Check out our How to Learn Python guide. This guide contains top tips on how to learn Python as well as a list of top courses and books to help you advance your Python knowledge.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.