print with variable justification (with *)
tom
tom at t0mb.net
Sat Nov 18 17:45:56 EST 2006
John Machin wrote:
> 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 :)
>
Sorry bout that !
> | >>> 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?
>
>
If you're on a unix system you can probably do `man fprintf`, but it
will mean wading through a lot of stuff you're no interested in. This
has a good little section on it though, specifically for python
http://rgruet.free.fr/PQR24/PQR2.4.html
> Cheers,
> John
>
>
More information about the Python-list
mailing list