print with variable justification (with *)
John Machin
sjmachin at lexicon.net
Sat Nov 18 17:38:20 EST 2006
tom wrote:
> Why Tea wrote:
> > print format % values
> > An optional minimum width of the conversion, specified using one or
> > more digits or an asterisk (*), which means that the width is taken
> > from the next item in values
> >
> > That's from one of O'reilly's books. But there is no example and I
> > couldn't get it to work by trials and errors. Does anyone have a
> > working example?
> >
> > /Why Tea
> >
> >
> value = 3.141592654
> print "%1.3f" % value
Please consider reading the subject of a message occasionally :)
| >>> value = 3.141592654
| >>> print "%1.3f" % value
| 3.142
| >>> print "%10.3f" % value
| 3.142
| >>> print "%*.3f" % (1, value)
| 3.142
| >>> print "%*.3f" % (10, value)
| 3.142
| >>> for n in range(11):
| ... print "%*.3f" % (n, value)
| ...
| 3.142
| 3.142
| 3.142
| 3.142
| 3.142
| 3.142
| 3.142
| 3.142
| 3.142
| 3.142
| 3.142
| >>>
> >
> there you go :)
There *you* go :)
> look at printf in c for general ideas about the format
> specifiers
Unfortunately this is about the same advice as given by the official
Python tutorial:
"""Most formats work exactly as in C [snip] Using * to pass the width
or precision in as a separate (integer) argument is supported"""
Not quite so many folks come to Python with a background in C these
days. Is anyone aware of a tutorial that covers % formatting from a
standing start?
Cheers,
John
More information about the Python-list
mailing list