Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get this string using python

Tags:

python

string

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

like image 205
zjm1126 Avatar asked Aug 30 '26 13:08

zjm1126


2 Answers

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

str.zfill

like image 61
user225312 Avatar answered Sep 01 '26 03:09

user225312


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']
like image 32
John La Rooy Avatar answered Sep 01 '26 04:09

John La Rooy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!