zjm1126
zjm1126

Reputation: 35680

how to get this string using python

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

Answers (5)

alex
alex

Reputation: 490637

Look at formatting strings in Python.

Upvotes: 1

John La Rooy
John La Rooy

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

primfaktor
primfaktor

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

Senthil Kumaran
Senthil Kumaran

Reputation: 56951

map(lambda x:str(x).zfill(5),a)

Upvotes: 1

user225312
user225312

Reputation: 131817

>>> a=[1000,200,30]
>>> [str(e).zfill(5) for e in a]
['01000', '00200', '00030']

str.zfill

Upvotes: 8

Related Questions