When we are working with different programs, there are different situations we might encounter when a list is given, and we need to convert that list to a string.
Python list to string
To convert a list to string in Python, use the join() function. The string join() is a built-in Python function that takes one parameter, the list, and returns the string.
The string join() method returns a string by joining all the elements of an iterable, separated by a string separator if provided.
# app.py data = ["Millie", "Bobby", "Brown", "is", "Enola", "Holmes"] def listToString(list): str1 = " " return (str1.join(list)) convertedStr = listToString(data) print(convertedStr)
Output
Millie Bobby Brown is Enola Holmes
In this code, we have defined a list and a user-defined function. The listToString() function accepts and passes the list to the join() method. The join() method is called upon an empty string and returns the string.
Python list of chars to string
To convert the list of chars to string in Python, use the join() method.
# app.py charList = ["M", "I", "L", "L", "I", "E"] charStr = ''.join(charList) print(charStr)
Output
MILLIE
But what if the list contains both string and int as its items?
Let’s see that scenario.
# app.py charList = ["M", "I", "L", "L", "I", "E", 1, 2, 3] charStr = ''.join(charList) print(charStr)
Output
Traceback (most recent call last): File "app.py", line 3, in <module> charStr = ''.join(charList) TypeError: sequence item 6: expected str instance, int found
We got an error like this. TypeError: sequence item 6: expected str instance, int found.
The solution is to convert it to the string while adding it to a string.
# app.py charList = ["M", "I", "L", "L", "I", "E", 1, 2, 3] listToStr = ''.join([str(elem) for elem in charList]) print(listToStr)
Output
MILLIE123
Converting a list to a string using the map() method
Python map() method executes the specified function for each element in an iterable. The element is sent to the function as a parameter.
# app.py strList = ["Millie", "Bobby", "Brown", "is", "Enola", "Holmes", 1, 2, 3] listToStr = ' '.join(map(str, strList)) print(listToStr)
Output
Millie Bobby Brown is Enola Holmes 1 2 3
Comma-separated string from a list of numbers
To convert a comma-separated string from the list, use the join() method.
# app.py strList = ["Millie", "Bobby", "Brown", "is", "Enola", "Holmes", 1, 2, 3] listToStr = ','.join(map(str, strList)) print(listToStr)
Output
Millie,Bobby,Brown,is,Enola,Holmes,1,2,3
Using the Python string strip() method
Python strip() method returns the copy of a string with both leading and trailing characters removed based on the string argument passed.
In our case, we will pass ‘[ ]’, then convert it into the string using the Python str() function.
See the following code.
# app.py stcast = ['millie', 'noah', 'finn'] print(type(stcast)) comcast = str(stcast).strip('[]') print(comcast) print(type(comcast))
See the output.
➜ pyt python3 app.py <class 'list'> 'millie', 'noah', 'finn' <class 'str'> ➜ pyt
That’s it for this tutorial.
hello, do you know how to get this result ?
“Millie”,”Bobby”,Brown”,”is”,”Enola”,”Holmes”,1,2,3
The idea is to still see the quotes in the output ?