Yubin Ruan writes: > Hi, everyone, I have some problem understand the rule which the python > compiler use to parsing the multiline string. > > Consider this snippet: > > str_1 = "foo" > str_2 = "bar" > > print "A test case" + \ > "str_1[%s] " + \ > "str_2[%s] " % (str_1, str_2) > > Why would the snippet above give me an "TypeError: not all arguments > converted during string formatting" while the one below not ? > > print "A test case" + \ > "str_1[%s] " % str1 > > Obviously the first snippet have just one more line and one more > argument to format than the second one. Isn't that unreasonable ? I > couldn't find any clue about that. Anyone help ?
Multiline is irrelevant. What is happening is that % binds more tightly (has a higher precedence) than +. print "foo" + "bar %s" % "baz" ==> foobar baz print "foo %s" + "bar %s" % ("baz", "baz") ==> Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: not all arguments converted during string formatting print ("foo %s" + "bar %s") % ("baz", "baz") ==> foo bazbar baz -- https://mail.python.org/mailman/listinfo/python-list