Python - Create String from List
Python - Create String from List
To create a string from a list in Python, you can use str() built-in function, string join() method, or a For loop.
In this tutorial, we shall go through each of these approaches, and see how to create a string from a given list, with examples.
1. Create string from list using str() in Python
str() built-in function can take a list as argument, and return the string representation of the list.
The resulting string contains square brackets, enclosing the items, and comma separator between the items.
In the following program, we take a list of three items in my_list, and create a string value from this list.
Python Program
# Given list
my_list = ["apple", "banana", "cherry"]
# Convert list to string
result = str(my_list)
# Print resulting string
print(result)
Output
['apple', 'banana', 'cherry']
2. Create string from list using string join() method in Python
Using string join() method, you can join the elements of the given list using a given separator string. The separator string can be an empty string, comma, or any valid string.
Creating a string by joining items in list with empty string as separator
In the following program, we take a list of three items in my_list, and create a string value from this list, by joining the items in the list, with an empty string separator.
Python Program
# Given list
my_list = ["apple", "banana", "cherry"]
# Convert list to string
result = "".join(my_list)
# Print resulting string
print(result)
Output
applebananacherry
Creating a string by joining items in list with comma as separator
In the following program, we take a list of three items in my_list, and create a string value from this list, by joining the items in the list, with an empty string separator.
Python Program
# Given list
my_list = ["apple", "banana", "cherry"]
# Convert list to string
result = ",".join(my_list)
# Print resulting string
print(result)
Output
apple,banana,cherry
3. Create string from list using For loop in Python
You can use a Python For loop to iterate over the items in the list, and then create a string by concatenating the items.
In the following program, we take a list of three items in my_list, and create a string value from this list, by iterating over the items in the list, and concatenating them to a resulting string.
While concatenating the items in the list to the resulting string, convert the item to string value.
Python Program
# Given list
my_list = ["apple", 25, 3.14]
# Convert list to string
result = ""
for item in my_list:
result += str(item)
# Print resulting string
print(result)
Output
apple253.14
Summary
In this tutorial, we learned how to create a string from a Python list. We have seen different approaches of creating a string from a given list. The first approach was to straight away use str() built-in function. The second approach was to use string join() method. And finally, the third approach was to use a For loop to iterate over the items in the list, and create a string by concatenating the items inside the loop.