[PyAR2] Thanks and another question...
Greg Lindstrom
gslindstrom at gmail.com
Fri Dec 21 18:19:30 CET 2007
On Dec 21, 2007 8:23 AM, W W <srilyk at gmail.com> wrote:
> Ah! Those examples were super helpful! Thanks!
Thanks. The formatters support everything one can do in "C". Some more
examples:
Minimum length of a string field (notice the '25' between the '%' and 's').
>>>t = 'this is a test'
>>> print '-->[%25s]<--' % w
-->[ this is a test]<--
Use a ''-' to left justify:
>>> print '-->[%-25s]<--' % w
-->[this is a test ]<--
You can specify the maximum length of a string field, too (notice the value
'.5'):
>>> print '-->[%.5s]<--' % w
-->[this ]<--
Combine the above two to force the string to be a certain length (in this
case 20 bytes):
>>> print '-->[%20.20s]<--' % w
-->[ this is a test]<--
You can do the same with integers, let's force a column to be 8-bytes wide
using the '%d' identifier
n = 123
>>> print '-->[%8d]<--' % n
-->[ 123]<--
And if you want leading zeros...
>>> print '-->[%08d]<--' % n
-->[00000123]<--
Floats allow you to specify a maximum with (before the decimal point) and
precision (after the decimal point). Use the %f format identifier.
f = 1234.56789
>>> print f
1234.56789
>>> print '-->[%6.2f]<--' % f
-->[1234.57]<-- notice it has been rounded.
Once you get the hang of it, you can look in the reference guide for a
mind-numbing list of format identifiers and qualifiers.
I believe python 3000 will change the print statement to a function and use
a different formatting style along the lines of:
>>> planet = 'World'
>>> name = 'Greg'
>>> print('Hello, {1}. My name is {2}' % (planet, name))
Hello, World. My name is Greg.
I'll try to find out more at PyCon in March.
--greg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/mailman/private/pyar2/attachments/20071221/4d094aab/attachment.htm
More information about the PyAR2
mailing list