Formatting numbers with leading zeros

Thomas D'Tak outpost at rumblefish.org
Sun Sep 19 16:19:05 EDT 2004


On Sun, 19 Sep 2004 12:59:21 -0700, Stian wrote:

> One thing I can't figure out is how
> to format numbers so that they get leading zeros
> 
> ... ... ...
>
> print '%sx%s'%(season,episode)
> 
> which gives the output 1x1, but I want 1x01

I think you are looking for the string method zfill(); 
you can find it here in Python's Library Reference:

  http://www.python.org/doc/2.3.4/lib/string-methods.html

If e.g. episode = '1', episode.zfill(2) will return '01' 
and episode.zfill(3) will return '001' etc. etc.

HTH, Th.




More information about the Python-list mailing list