The Random Ramblings

lost? dazed? confused? dont come here then as this aint goona help!!

More python, padding zeros on string and numbers

Why am I putting this here you might ask? Well its something that has been in and out of my script numerous times and each time I have to look it up so thought I would drop it here where I can find it every time. Also I'm hoping that by tying it in here I will remember and never have to come back anyway!!

for strings:

>>>> n = '19'
>>>> print n.zfill(3)
>>>> '019'

for numbers:

>>>> n = 19
>>>> print "%03d" % n
>>>> 019

The above method is supposedly depreciated so

>>>> print "{0:03d}".format(n) #python >= 2.6
>>>> 019
>>>> print ("{0:03d}".format(n)) #python 3
>>>> 019

just a little snippet that might help all of us.

Find Me
Category
100daysofcode
Alexa
films
Javascript learning