To convert list to string in python follow any one of the below methods which helps you as per your need. Below are the 4 methods to convert list to string in python.
Contents
Method – 1
In this method string iterates through for loop and adds its element to string variable.
def convertListtoString(lst):
string = '' #empty string
for i in lst:
string += i
return string
#list that needs to be converted
lst = ["Welcome"," to ", " SimilarGeeks!"]
print(convertListtoString(lst))
Output:
Welcome to SimilarGeeks!
Method – 2
Using .join() method to convert the list into string.
def convertListtoString(lst):
string = ''
return (string.join(lst))
lst = ["Welcome"," to ", " SimilarGeeks!"]
print(convertListtoString(lst))
Output:
Welcome to SimilarGeeks!
When a list contains both string and integer elements, the above procedure is not recommended.
Method – 3
Using map() function
lst = ["Welcome"," to ", " SimilarGeeks!"]
# using map funtion
convertListtoString = ' '.join(map(str,lst))
print(convertListtoString)
Output:
Welcome to SimilarGeeks!
Method – 4
Using list comprehension
lst = ["Welcome"," to ", " SimilarGeeks!"]
#List comprehension
convertListtoString = ' '.join([str(i) for i in lst])
print(convertListtoString)
Output:
Welcome to SimilarGeeks!
Also Read:
Program to Reverse a string in python