Reputation: 35680
i have a list like this :
a=[1000,200,30]
and i want to get a list like this :
['01000','00200','00030']
so what can i do ,
thanks
Upvotes: 2
Views: 151
Reputation: 304473
str.format()
is the preferred way to do this if you are using Python >=2.6
>>> a=[1000, 200, 30]
>>> map("{0:05}".format, a)
['01000', '00200', '00030']
Upvotes: 5
Reputation: 2999
You can do it like this:
a = [1000,200,30]
b = ["%05d" % (i) for i in a]
print b
The number tells the width and the leading zero says that you want leading zeros.
Upvotes: 2
Reputation: 131817
>>> a=[1000,200,30]
>>> [str(e).zfill(5) for e in a]
['01000', '00200', '00030']
Upvotes: 8