Python arrays and sting formatting options

Chris Rebert clp at rebertia.com
Mon Sep 29 22:03:51 EDT 2008


On Mon, Sep 29, 2008 at 6:56 PM, Ivan Reborin
<ireborin at delete.this.gmail.com> wrote:
> On Mon, 29 Sep 2008 17:59:40 -0700 (PDT), bearophileHUGS at lycos.com
> wrote:
>
> Hello bearophile, thank you for replying.
>
>>The Python genie grants you that wish. You were almost right:
>>>>> print (3 * '%12.3f') % (a, b, c)
>>       2.000  123456.789    1234.000
>>>>> print 3 * '%12.3f' % (a, b, c)
>>       2.000  123456.789    1234.000
> Works beautifully :-) Thank you!
>
>>>>> print 3 * '%12.3f' % a, b, c
>>Traceback (most recent call last):
>>  File "<stdin>", line 1, in <module>
>>TypeError: not enough arguments for format string
>
> Just one more question - it's actually an extension to this one
> (forgive my curiosity, but I really need this info, and searching
> google always gives me the same stuff again and again) ...
>
> a = 2.000001
> b = 123456.789
> c = 1234.0001
> d = 98765.4321
> # same as above except for d
>
> print (3 * '%12.3f') % (a, b, c)
> #this works beautifully
>
> How to add d at the end but with a different format now, since I've
> "used" the "format part" ?
>
> Again, my weird wishful-thinking code:
> print (3*'%12.3f', '%5.3f') %(a,b,c),d

Again, very close to the correct code:

print (3*'%12.3f' + '%5.3f') %(a,b,c,d)

Regards,
Chris

>
>
>>(Note the spaces and parentheses. Python programmers thank you if put
>>them improving readability a little).
>
> Yes, ok. I can agree with that - separating the format from the
> variable list part sounds reasonable.
>
>>
>>Bye,
>>bearophile
>
> --
> Ivan
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list