473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

print with variable justification (with *)

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
6 1246
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
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
In [50]: '%*s' % (5, 'spam')
Out[50]: ' spam'
Marc, that's exactly what I need. Thanks!

/Why Tea

Nov 18 '06 #4
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Balaji | last post by:
Hello Eveybody, I have written a method which prints the prefix notation of any expression. here is the method... def PrintPrefix(expr): if expr.__class__==E: print expr.operator,...
6
by: Ruchika | last post by:
Hi, I am new to Python, so this may be a very simple question for most of you. What does the % operator stand for in Python? I came across a script that uses the % operator as follows - def...
23
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two...
18
by: Joe | last post by:
Hi, I am trying to alter the refresh rate of an online webpage in a webbrowser control using MFC. However the Timer ID is stored in a local variable and I don't know how to access it. Is there a...
1
by: AMD 3400 | last post by:
Hello there, i have a question to ask, i have a huge 5000 columns in Excel that i've been working on and i want it right-justified. When i open it in Access it gives me left-justified. i would...
19
by: Victor Nazarov | last post by:
Consider I want to write my own version of standard malloc, calloc, realloc, free. How can I portably check if they work correctly? The most remarkable will be the test for right justification of...
0
by: vbmania | last post by:
Hello, I am going to print paper of 15 X 4 Inch. It's a continuous form. But When I Print Multiple Paper, Printer does'nt print back to back. If I am goint to print 3 pages. Jump over...
4
by: Neil | last post by:
Just found out that the Microsoft Rich Textbox does not support full text justification, since it's based on Version 1.0 of the RichEdit Window Class, and full text justification is only available...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.