[Tutor] calculation in string operations

Sander Sweers sander.sweers at gmail.com
Thu Aug 4 02:22:55 CEST 2011


On 4 August 2011 01:16, David <ldl08 at gmx.net> wrote:
> I got an error message that surprises me. I know how to solve the
> problem (couple of parentheses will do), but I would expect Python to
> run as the code is presented below.
> After all, my_weight is a number that can be multiplied by a float.
> I would expect Python to evaluate the expression after the (final)
> operator and then having it inserted into the string.
>
> So I guess my question is this: what is the path that Python follows in
> its string formatting operations?

It had my stumped at first but then I saw what was happening. After
some experimentation it seems that string formatting first converts
my_weight to a string and then multiplies it. And multiplying a string
with a float is not possible.

Greets
Sander


More information about the Tutor mailing list