How To Convert List Into String in python
Posted on July 7, 2023  (Last modified on July 11, 2023 )
3 minutes • 587 words
Table of contents
How To Convert a List Into a String in Python
In this tutorial, we will learn about how to convert a list into a string with two different methods.
Why do we convert a list into a string?
Converting a list into a string in Python is useful when you want to combine the elements of the list into a single text. It helps you manipulate the list as a cohesive unit, making it easier to work with and display the data in a desired format.
This is the different method we can use to convert our list into a string:
- Using .join() fucntion
- Using for loop
Method 1: Using .join()
The .join() function is a string method in Python that allows us to concatenate the elements of an iterable into a single string. It takes the form of separator, and iterable. Here’s how .join() works:
Separator: The separator is a string that specifies how the elements should be separated in the resulting string. It is inserted between each pair of elements during the concatenation process. For example, if the separator is ’ ‘, the elements will be joined with a space between them. Iterable: The iterable represents the collection of elements that you want to join. It can be any iterable object, such as a list.
Here’s an example:
my_list = ["make", "coding", "simple"]
new_string = ' '.join(my_list)
print(new_string)
Output:
make coding simple
Another example using the .join() function:
my_list = ["make", "coding", "simple"]
new_string = ', '.join(my_list)
print(new_string)
Output:
make, coding, simple
In the above example, ‘, ‘.join(my_list) joins the elements of my_list into a single string with the specified separator ‘, ‘. The resulting string is stored in the new_string variable and printed to the console.
Method 2: Using for loop
A for loop can be used to convert a list into a string by iterating over each element of the list and adding them together manually.
This method isn’t the best for converting a list into a string, but it can help you to understand more about for loop.
Here’s a simple explanation of how to achieve that:
my_list = ["make", "coding", "simple"]
my_string = ""
for item in my_list:
my_list += item
print(my_string)
Output:
makecodingsimple
In the above example, we first need to make an empty string to store the result. Then we use for loop to iterate for each item in my_list. In the loop, we add or append each of the item to my_string using the += operator.
Another example if you want to have a separator between each item
my_list = ["make", "coding", "simple"]
my_string = ""
for item in my_list:
my_string += item + ' '
print(my_string)
Output:
make coding simple
In the updated example above, we append each item to the result string along with the separator ’ ‘.
Conclusion :
In this tutorial we used two methods for converting a list into a string in Python: using the .join() function and using a for loop. The .join() function simplifies the process by allowing you to add list elements with a specified separator. Alternatively, a for loop can be used to manually build the string by iterating over each element in the list. Both methods offer flexibility in manipulating and displaying list data as a string.
Share
Other Tutorial
Read Previous | Read Next |
---|---|
How To Calculate Average From A List In Python | How To Remove A Duplicate From A List In Python |