+ 4
How to format list to str? Python
For example: [1,2,3,4,5] To "12345"
5 Réponses
+ 7
lst =[1, 2, 3, 4, 5]
print(''.join(str(i) for i in lst))
# 12345
+ 7
l = [1,2,3,4,5]
s = ''.join(map(str, l))
print(s) # '12345'
+ 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.
+ 5
l = [1,2,3,4,5]
print(*l, sep = '')
# result: 12345
0
Help