Python arrays and sting formatting options

Ivan Reborin ireborin at delete.this.gmail.com
Tue Sep 30 03:56:03 CEST 2008


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


>(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



More information about the Python-list mailing list