Javascript required
Skip to content Skip to sidebar Skip to footer

How to Convert a List to a String in Python

3 Answers 3

By using ''.join

                list1 = ['1', '2', '3'] str1 = ''.join(list1)                              

Or if the list is of integers, convert the elements before joining them.

                list1 = [1, 2, 3] str1 = ''.join(str(e) for e in list1)                              

jamylak

117k 27 gold badges 221 silver badges 224 bronze badges

answered Apr 11 '11 at 8:52

6

  • @SenthilKumaran If the resulting string requires separate items such as when passing a list of files to a process, then "" has to be changed to " " (notice the space between the quotes). Otherwise, you end up with a contiguous string ("123" instead of "1 2 3"). This is OK if that was the intention, but needs to be mentioned in the response.

    May 9 '16 at 19:43

  • Just gonna point out that the second form works fine on almost any (single-depth) list.

    Aug 29 '16 at 4:03

  • I was looking for this here and found it elsewhere: If you want to have a newline for every list element (might be useful for long-string lists): print ("\n".join(['I', 'would', 'expect', 'multiple', 'lines']))

    Mar 3 '18 at 11:55

  • The question may be a duplicate but the answer here is better than there. :) Plus this pops up as the top result on google unlike the other. (which I've noticed a lot with dupes)

    Sep 19 '18 at 1:14

  • Agree with @Bogdan. This answer creates a string in which the list elements are joined together with no whitespace or comma in between. You can use ', '.join(list1) to join the elements of the list with comma and whitespace or ' '.join(to) to join with only white space

    Sep 28 '18 at 6:32

                >>> L = [1,2,3]        >>> " ".join(str(x) for x in L) '1 2 3'                              

answered Apr 11 '11 at 8:53

1

  • I'd recommend to use: >>> " ".join([str(x) for x in L])

    Feb 13 at 18:53

                L = ['L','O','L'] makeitastring = ''.join(map(str, L))                              

kame

18k 29 gold badges 96 silver badges 148 bronze badges

answered Apr 11 '11 at 8:56

3

  • it throw TypeError exception,like this: In [15]: L=['1','2','3'] In [16]: print ''.join(map(str,L)) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-16-f3b8e6cb2622> in <module>() ----> 1 print ''.join(map(str,L)) TypeError: 'str' object is not callable

    Jan 6 '13 at 11:44

  • Since it wasn't specific in the question... If you want to preserve single string quotes on your list items (for sending a query to SQL, for instance), you can do something like this. x = [1,2,3] y = "'" + "','".join(map(str, x)) + "'"

    Mar 5 '15 at 16:29

  • Anyone who is trying to re-use the string after converting the list to string can you this method: list1 = [1, 2, 3] str1 = ''.join('hello world' + str(e) + 'hello world' for e in list1) Output =========== hello world 1 hello world hello world 2 hello world hello world 3 hello world

    May 16 '19 at 5:19

Not the answer you're looking for? Browse other questions tagged python string list or ask your own question.

How to Convert a List to a String in Python

Source: https://stackoverflow.com/questions/5618878/how-to-convert-list-to-string