1160

How can I convert a list to a string using Python?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Nm3
  • 11,707
  • 3
  • 16
  • 4
  • 60
    `str(anything)` will convert any python object into its string representation. Similar to the output you get if you do `print(anything)`, but as a string. – ToolmakerSteve Jan 27 '16 at 20:17
  • 3
    So one way to make this different than that one is to suggest using json to do it like https://stackoverflow.com/questions/17796446/convert-a-list-to-a-string-and-back. Using json easily allows the reverse process (string to list) to take place. But the OP really did need to explain themselves. Is it for display only or some other purpose? – demongolem Oct 02 '18 at 16:58
  • Please give an example of the format you want. – Solomon Ucko Mar 12 '20 at 01:21

3 Answers3

1928

Use ''.join:

xs = ['1', '2', '3']
s = ''.join(xs)

If the list contains integers, convert the elements to string before joining them:

xs = [1, 2, 3]
s = ''.join(str(x) for x in xs)
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • 57
    @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. – Bogdan May 09 '16 at 19:43
  • Just gonna point out that the second form works fine on almost any (single-depth) list. – The Nate Aug 29 '16 at 04:03
  • 6
    I was looking for this here and found it [elsewhere](https://stackoverflow.com/questions/14560863/python-join-with-newline): 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']))` – Honeybear Mar 03 '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) – codehelp4 Sep 19 '18 at 01:14
  • 5
    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 – R.S.K Sep 28 '18 at 06:32
  • You can use it with parsing all of the cmd arguments, with `test = sys.argv[1:]` and then the `join` method. – Timo Dec 22 '20 at 18:38
  • `str(anything)` will convert any python object into its string representation. Similar to the output you get if you do print(anything), but as a string. – Redoman Sep 18 '22 at 17:30
419
>>> xs = [1, 2, 3]       
>>> " ".join(str(x) for x in xs)
'1 2 3'
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Andrey Sboev
  • 7,454
  • 1
  • 20
  • 37
129
xs = ['L', 'O', 'L']
lol_string = ''.join(map(str, xs))
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Nathan
  • 6,095
  • 10
  • 45
  • 54
  • 2
    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) in () ----> 1 print ''.join(map(str,L)) TypeError: 'str' object is not callable – wgzhao Jan 06 '13 at 11:44
  • 17
    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)) + "'"` – tandy Mar 05 '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 – PanDe May 16 '19 at 05:19
  • `str(anything)` will convert any python object into its string representation. Similar to the output you get if you do print(anything), but as a string. – Redoman Sep 18 '22 at 17:30