Introduction to Python List to String
In Python, lists are similar to arrays and can have elements of any data type. List in simple words is a collection of elements in a sequenced order, with each element having indexes. Strings in Python are data types like other data types such as int, char, etc., and string, in general, can be defined as a sequence of characters. So in this article, we will see how to convert lists into strings in the Python programming language, and there are several other ways of doing this.
How to Convert List to String with Examples
In this article, we introduced to what are lists and string in Python. Now let us see how to convert lists to string in Python. There are different methods to convert lists to string in Python.
Let us see each method with examples below:
1. List to String
As the name itself suggests, this method is used when we have created an empty string, then we can convert the list to string by just iterating through the list and adding the elements respective to the index in this empty string, and this can be shown in the below example.
Code:
print("Program to convert a list to string:")
print("\n")
def func(s):
str1 = ""
for e in s:
str1 += e
return str1
s = ['Educba', 'Training', 'Institute']
print("The given list is as follows:")
print(s)
print("\n")
print("The given list is converted to string as follows:")
print(func(s))
Output:
In the above program, we can see we have defined a function func() in which we have passed a list named as “s” inside the function we have initialized an empty string “str1” then using “for” loop, we traverse through the string and return the string by converting the list that was passed to this function and then the output gives the string by combining all the elements of the given list.
2. By Using .join() Method
In Python, there is a method for string known as the join() method, and this method can also be used to convert the given list to string.
Syntax:
str.join(iterable)
Here iterable can be a list, set, tuple, etc.
Code:
print("Program to convert a list to string using join() function:")
print("\n")
def func(s):
str1 = " "
return (str1.join(s))
s = ['Educba', 'Training', 'Institute']
print("The given list is as follows:")
print(s)
print("\n")
print("The given list is converted to string as follows:")
print(func(s))
Output:
In the above program, we can see a defined function “func()” where we have passed a list “s”; this function is for conversion. Firstly in this function, we initialize an empty string and then, using the join() method, we convert the given list to a string. One thing to note is that using the join() method will append space after every element of the list and then joins the list elements to one single string.
3. Using List Comprehension
There might occur a situation where a list can contain string and integers, then how to convert such lists to a string? So we need to add string while adding to a string. This can be done using this method.
Code:
print("Program to convert a list to string using list comprehension:")
print("\n")
s = ['Educba', 'Training', 'has', '10', 'courses', 'with', '2', 'subcourses', 'for', 'each', 'course']
print("The given list is as follows:")
print(s)
print("\n")
listToString = ' '.join([str(e) for e in s])
print("The given list is converted to string is as follows:")
print(listToString)
Output:
In the above program, we can see we have a list defined which has elements as both sting and integers. So to convert these kinds of lists, we use this comprehension method where we use both join() method and “for” loop in one single statement and print the entire elements of lists as a string. The output is as shown in the screenshot.
4. Using map() Method
This method is used for converting the list to string for mapping string with the given list. This method is used when the list has nonstring elements where these elements are converted to a string first. We have seen the above example also doing the same work but using the map() method.
Code:
print("Program to convert a list to string using list comprehension:")
print("\n")
s = ['Educba', 'Training', 'has', '10', 'courses', 'with', '2', 'subcourse','for', 'each']
print("The given list is as follows:")
print(s)
print("\n")
listToString = ' '.join(map(str, s))
print("The given list is converted to string is as follows:")
print(listToString)
Output:
In the above program, we can see the same working as the program using the comprehension method, but here we use the map() function to convert the list into a string, and the result is as shown in the screenshot above.
Conclusion
In this article, we conclude that the conversion of the list to string is simple in Python. In this article, we saw various methods for converting the given list to a string. At first, we saw by adding elements to the empty string from the given list; second, we saw the use of join method for conversion; third, we saw using comprehension method using join() and lastly, we saw the same comprehension method with the same comprehension map() method.