print with variable justification (with *)

W

Why Tea

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
 
M

Marc 'BlackJack' Rintsch

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?

In [50]: '%*s' % (5, 'spam')
Out[50]: ' spam'

In [51]: '%*s' % (10, 'spam')
Out[51]: ' spam'

In [52]: '%*s' % (15, 'spam')
Out[52]: ' spam'

Ciao,
Marc 'BlackJack' Rintsch
 
T

tom

Why said:
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


there you go :) look at printf in c for general ideas about the format
specifiers
 
J

John Machin

tom said:
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
 
T

tom

John said:
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
 
J

John Machin

tom said:
John Machin wrote:
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

Well done, tom, you've redeemed yourself! Those formatting examples are
just the ticket. I used that quick reference years ago when I started
out with Python, but I wasn't aware that it was still maintained, and
an advertisement for CSS as well. Bonus!

Cheers,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top