Correct behavior?

DanBishop04 at gmail.com DanBishop04 at gmail.com
Thu Apr 26 20:53:48 EDT 2007


On Apr 26, 8:34 pm, asker <vizcay... at gmail.com> wrote:
> Hello:
> I have next Python 2.5.1 instructions:
>
> >>> a=12.34
> >>> b=11.23
> >>> print a+b
>
> 23.57
>
> >>> print "%15.2f" % (a+b)
>
>           23.57
>
> But:>>> print "%15.2f" % a+b
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: cannot concatenate 'str' and 'float' objects
>
> Is this correct for Python to issue this error?

The % operator has higher precedence than +.  Thus, "%15.2f" % a+b ==
("%15.2f" % a)+b, an illegal str+float addition.




More information about the Python-list mailing list