On 6/2/2017 1:28 PM, Jussi Piitulainen wrote:
sean.diza...@gmail.com writes:

Can someone please explain this to me?  Thanks in advance!

~Sean


Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
print "foo %s" % 1-2
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'int'
The per cent operator has precedence over minus. Spacing is not
relevant. Use parentheses.


In other words "foo %s" % 1 is executed, giving "1". Then "1"-2 is attempted giving the error. Also: If there is more than one conversion specifier the right argument to % must be a tuple. I usually write a tuple even if there is only one conversion specifier - that avoids the problem you encountered and makes it easy to add more values when you add more conversion specifiers.

print "foo %s" % (1-2,)

Bob Gailer

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to