Escaping double quotes in a string


#1

Please leave the following link in the post so we can find the unit module, else replace it with a link to the exact lesson:

Strings and Console Output

How is it possible that they have added backslash \ in their code and escaping double quotes before and
And adding one double quote before it escapes character.
String read like this:
print "Ah, so your name is %s, your quest is %s, " “and your favorite color is %s.” % (name, quest, color)
This one must give error, but it’s not. Why ?


#2

This is not escapement, but inclusion. (Oddly enough, we could be escaping the newline, \n, so I won’t be arguing this point.)

print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, quest, color)

In the above instance, \ means continuation. The interpreter recoginzes it inside or outside of strings, and treats them both the same way.

print "Ah, so your name is %s, your quest is %s, \
and your favorite color is %s." % (name, quest, color)

Bottom line in either instance, the \ must be the last printable character on that physical line. It cannot have anything after it.


The obvious reason for this operator is so we do not need to scroll horizontally to read our code or large bodies of text in our code. With the operator as the last character on the line it permits a line break while excluding it (the escape sequence) from the text.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.