[Tutor] makeing '1' into a '01' neatly ?

Michael Lange klappnase at freenet.de
Wed Apr 28 06:52:49 EDT 2004


On Wed, 28 Apr 2004 12:17:20 +0200
Kalle Svensson <kalle at lysator.liu.se> wrote:

> [Dave S]
> > I need strings of the form 01, 02, 03 ... ,10,11,12 ... for
> > archiving files.
> > 
> > Is there an neat way to format a '1' into a '01' ?
> 
> >>> for x in '1', '3', '30':
> ...     print '%02d' % int(x)
> ... 
> 01
> 03
> 30
> 
> Peace,
>   Kalle
> -- 

Or just:

>>> x = '1'
>>> x.zfill(2)
'01'
>>> y = '11'
>>> y.zfill(2)
'11'
>>> 

Regards

Michael



More information about the Tutor mailing list