"Yubin Ruan" wrote in message
news:930753e3-4c9c-45e9-9117-d340c033a...@googlegroups.com...
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 ?
This has nothing to do with multi-line strings.
Try that as a single line -
print "A test case " + "str_1[%s] " + "str_2[%s]" % (str_1, str_2)
You will get the same error message.
The reason is that the use of the '+' sign to concatenate strings requires
that each sub-string is a valid string in its own right.
The correct way to write it is -
print "A test case " + "str_1[%s] " % (str_1) + "str_2[%s]" % (str_2)
If you wrote it without the '+' signs, the answer would be different. Python
treats contiguous strings as a single string, so you could write it like
this -
print "A test case " "str_1[%s] " "str_2[%s]" % (str_1, str_2)
Frank Millman
--
https://mail.python.org/mailman/listinfo/python-list