[Tutor] Formatter

Bob Gailer ramrom@earthling.net
Sat Jan 18 11:49:02 2003


--=======7A544C95=======
Content-Type: text/plain; x-avg-checked=avg-ok-B74577E; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 8bit

At 11:05 AM 1/18/2003 -0500, Pete Versteegen wrote:
>I have selected Python as a language to be
>learned thoroughly because of its capabilities to do scientific work, web
>work, and developing GUIs.

Not to mention that its a LOT easier than C++

>Does anyone know of an easy way to format a mixture of floats, integers,
>spaces, and text in well arranged columns?
>
>My thoughts wander towards the way the Fortran language does it.  I'm
>considered a function (class) that would be called as follows:
>format((a, alf, i), "f10.2, a10, 10x, i5")
>This would print the three items in the tuple according to the specifactions
>listed between the quote marks, whee
>f10.2 (float, 10 wide, 2 digits after decimal point)
>a10 (alpha number of 10 wide
>10x (1o blank spaces)
>i5 (integer of 5 digits wide)

See http://www.python.org/doc/current/lib/typesseq-strings.html#l2h-148 for 
string formatting. You can probably get what you need there.
For your example:
print '%10.2f%10s          %5d' % (2.3, 'asdf', 4) # gives 
"      2.30      asdf              4"

Bob Gailer
mailto:ramrom@earthling.net
303 442 2625

--=======7A544C95=======
Content-Type: text/plain; charset=us-ascii; x-avg=cert; x-avg-checked=avg-ok-B74577E
Content-Disposition: inline


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.438 / Virus Database: 246 - Release Date: 1/7/2003

--=======7A544C95=======--