Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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
>>> a=[1000,200,30] >>> [str(e).zfill(5) for e in a] ['01000', '00200', '00030']
str.zfill
Add a comment
-0030
str.format() is the preferred way to do this if you are using Python >=2.6
str.format()
>>> a=[1000, 200, 30] >>> map("{0:05}".format, a) ['01000', '00200', '00030']
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.
map(lambda x:str(x).zfill(5),a)
str.format
Look at formatting strings in Python.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.