By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,035 Members | 1,984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

print with variable justification (with *)

P: n/a
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

Nov 18 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In <11**********************@m73g2000cwd.googlegroups .com>, 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?
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
Nov 18 '06 #2

P: n/a
tom
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
there you go :) look at printf in c for general ideas about the format
specifiers
Nov 18 '06 #3

P: n/a
In [50]: '%*s' % (5, 'spam')
Out[50]: ' spam'
Marc, that's exactly what I need. Thanks!

/Why Tea

Nov 18 '06 #4

P: n/a
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

Nov 18 '06 #5

P: n/a
tom
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

Nov 18 '06 #6

P: n/a

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

Nov 18 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.