[Tutor] String formating

Steven D'Aprano steve at pearwood.info
Fri Dec 16 01:46:30 CET 2011


Stayvoid wrote:
> Hey folks!
> 
> What's the difference between these commands?
> 
> print "%02d" % (12)
> 
> print "%d" % (12)
> 
> I know that "d" stands for decimal. What does "02" mean?

"0" means to use leading zeroes; "2" means to use 2 digits. Here's a better 
example:


py> "%05d" % 12
'00012'


More information here:

http://docs.python.org/library/stdtypes.html#string-formatting-operations

-- 
Steven


More information about the Tutor mailing list