How do I convert numbers in a list into strings?

for example:

input : (000,111,6272,322,222)

output : ("000","111","6272","322","222")

3 Answers

1
Sigve Karolius On Best Solutions

You have the problem that the integer 000 is the same as 0, meaning that python will appear to change the number when converting:

lst = [000,111,6272,322,222]
map(str, lst)
# ['0', '111', '6272', '322', '222']

However , in reality when you define a variable as 000 python ignores the redundant zeroes:

a = 0000000
print(a)
# 0
0
Mohamed Benkedadra On

do the following :

a = [000, 111, 6272, 322, 222]

b = [str(i) for i in a]

print(b)

output : ["0", "111", "6272", "322", "222"]

2
Klemen Koleša On

I am assuming you are talking about list in Python. Just convert every element in the list to string.

You can do it like this with list comprehension:

l = [0,111,6272,322,222]
l_str = [str(item) for item in l]