How to format list to str? Python | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 4

How to format list to str? Python

For example: [1,2,3,4,5] To "12345"

19th May 2019, 4:32 PM
Денис Балуев
Денис Балуев - avatar
5 Antworten
+ 7
lst =[1, 2, 3, 4, 5] print(''.join(str(i) for i in lst)) # 12345
19th May 2019, 4:40 PM
Diego
Diego - avatar
+ 7
l = [1,2,3,4,5] s = ''.join(map(str, l)) print(s) # '12345'
19th May 2019, 4:41 PM
Anna
Anna - avatar
+ 6
You can also use loops: list_x = [1, None, True, "Hello"] string_x = "" for i in list_x: string_x += str(i) "1NoneTrueHello" But often using "".join(list) is more recommended, because string is an immutable object, each time you ran string_x += str(i) would destroy and create a new string in each iteration of the loop. I would expect builtin str.join to perform it smarter.
19th May 2019, 7:10 PM
Seb TheS
Seb TheS - avatar
+ 5
l = [1,2,3,4,5] print(*l, sep = '') # result: 12345
19th May 2019, 4:58 PM
Lothar
Lothar - avatar
0
Help
18th Jun 2019, 5:02 PM
Zia Khan
Zia Khan - avatar