Multiline parsing of python compiler demistification needed

Yubin Ruan ablacktshirt at gmail.com
Thu Jun 16 04:34:33 EDT 2016


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 ?

I am using python 2.7.6

Also, I know the **Pythonic** way to play with multiline, which would be using triple quote or a pair of parenthesis to surround the multiline string to make it a **online** string. You don't have to show code in that respect.

Thanks in advance!

Ruan.



More information about the Python-list mailing list