Lets say you have a list like ['a', 'b', 'c']
and you want it to look like 'abc'
and you don't want to use some big stupid looking for loop.
To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.
If you want to concatenate a list of numbers ( int or float ) into a single string, apply the str() function to each element in the list comprehension to convert numbers to strings, then concatenate them with join() .
Use the join() Method to Convert the List Into a Single String in Python. The join() method returns a string in which the string separator joins the sequence of elements. It takes iterable data as an argument. We call the join() method from the separator and pass a list of strings as a parameter.
>>> str_list=['a','b','c']
>>> ''.join(str_list)
'abc'
I think it's
string = ''.join(['a', 'b', 'c'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With