Newbie Formatting Question
Heather Coppersmith
me at privacy.net
Mon Mar 22 18:51:35 EST 2004
On Mon, 22 Mar 2004 16:33:34 GMT,
. <soyyo at NOSPAMterra.es> wrote:
> I'm going through "How to Think Like a Computer Scientist", and
> am in chapter 11. (aside: what do you think of the book? any
> recommendations for other books?)
Can't help here.
> print "%-20x %12.02f" % (student, wages[student])
> I understand what's going on, and the example works fine. I
> just don't understand what the 0 is doing in the %12.02f format
> specification. I thought it might be to force padding zeroes,
> but I get the same result with or without the 0. Also, %12.0002f
> gives the same result. Was this a typo, or is there something
> that I'm not understanding?
The number after the decimal point is the number of digits that
should come out (surprise!) after the decimal point:
>>> for x in range( 10 ):
f = '%20.' + str( x ) + 'f' # or f = '%%20.%df' % x
print f % 1.2
1
1.2
1.20
1.200
1.2000
1.20000
1.200000
1.2000000
1.20000000
1.200000000
The padding/filling comes for free, as part of the format string
containing a number after the decimal poiint. So '%6.2f',
'%6.02f', and '%6.00002f' mean exactly the same thing, namely to
put 2 digits after the decimal point (and if that means padding
with zeroes, then so be it).
HTH,
Heather
--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli
More information about the Python-list
mailing list