Newbie Formatting Question

Garry Knight garryknight at gmx.net
Mon Mar 22 16:30:30 EST 2004


In message <MPG.1ac933d4aaaa7d02989680 at news.teleline.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?)

I thought it was a good introduction both to programming and to Python. Here
are a few others:

Learning to Program: http://www.freenetpages.co.uk/hp/alan.gauld/
A Byte of Python: http://www.python.g2swaroop.net/
Dive Into Python: http://diveintopython.org/
Text Processing in Python: http://gnosis.cx/TPiP/
Python Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/
An Introduction to TkInter:
http://www.pythonware.com/library/tkinter/introduction/index.htm

And more: http://www.tcfb.com/freetechbooks/bookphyton.html

> I have a question about an example of formatting in the book
> (page 120). There is a line that says:
> 
> 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?

I don't know if it was a typo or whether it's just the author's habit to put
an extra 0 in there. It works fine with or without it for any number I
could throw at it. The C sprintf() function uses a similar specification in
which the 0 in that position does (AFAICR) mean 'pad with zeroes'. A
specification of "%12.02f" would yield "1.20" while a specification of
"%12.2f" would yield "1.2". Python seems to force the zero-padding, or it
doesn't use sprintf() maybe...

-- 
Garry Knight
garryknight at gmx.net  ICQ 126351135
Linux registered user 182025



More information about the Python-list mailing list