Quick: Format Specs in %

John Machin sjmachin at lexicon.net
Wed Apr 24 22:41:21 EDT 2002


Philip Swartzleonard <starx at pacbell.net> wrote in message news:<Xns91FA8B766EF98RASXnewsDFE1 at 130.133.1.4>...
> '%2f' % some_number pads the number with 2 blank spaces
> '%.2f' % some_number fixes the number to 2 decimal places
> 
> the problem:
> '%2.2f' % some_number only does the second of those two things. I need 
> both. I tried looking up the % operator in the index of the language 
> reference, but couldn't find it.

The language reference manual talks about % only as a numeric
remainder/modulus operator. What you are looking for is cunningly
hidden in the library reference manual, in "2.2.6.2 String Formatting
Operations".

If you have a Windows PC, grab a copy of Hernan Foffani's help kit in
Windows .chm format (or grab the Activestate distribution of Python
instead of the python.org distribution) -- the .chm allows
search/indexing across all the manuals.

For the meaning of say %8.2f, given the heritage, you could look in
any C book under "printf" -- interestingly, Python seems to have also
cloned C's documentation methodology: K&R v2 has an entry for "%
modulus operator" in its index, but not "% thingy in printf and
scanf".



More information about the Python-list mailing list