banner



How To Change A List To A String In Python

3 Answers 3

By using ''.bring together

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

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

                list1 = [one, ii, 3] str1 = ''.join(str(east) for e in list1)                              

user avatar

jamylak

119k 28 gold badges 223 argent badges 224 bronze badges

answered Apr 11, 2011 at viii:52

user avatar

6

  • @SenthilKumaran If the resulting string requires split items such every bit when passing a list of files to a procedure, then "" has to be changed to " " (detect the space between the quotes). Otherwise, you end upwardly with a contiguous cord ("123" instead of "1 2 three"). This is OK if that was the intention, but needs to be mentioned in the response.

    May nine, 2016 at 19:43

  • But gonna point out that the second class works fine on nigh any (single-depth) list.

    Aug 29, 2016 at 4:03

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

    Mar 3, 2018 at eleven:55

  • The question may be a duplicate merely the answer hither is better than there. :) Plus this pops up every bit the top result on google dissimilar the other. (which I've noticed a lot with dupes)

    Sep 19, 2018 at 1:14

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

    Sep 28, 2018 at 6:32

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

answered Apr 11, 2011 at viii:53

user avatar

i

  • I'd recommend to use: >>> " ".bring together([str(ten) for 10 in L])

    Feb thirteen, 2021 at 18:53

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

user avatar

kame

xviii.6k 30 aureate badges 98 silver badges 148 bronze badges

answered Apr 11, 2011 at 8:56

user avatar

3

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

    January vi, 2013 at 11:44

  • Since it wasn't specific in the question... If you want to preserve unmarried string quotes on your listing items (for sending a query to SQL, for instance), you can practise something like this. ten = [one,2,iii] y = "'" + "','".bring together(map(str, x)) + "'"

    Mar 5, 2015 at 16:29

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

    May sixteen, 2019 at 5:19

Non the respond y'all're looking for? Scan other questions tagged python string listing or inquire your own question.

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

Posted by: ortegabeent1988.blogspot.com

0 Response to "How To Change A List To A String In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel