python - Quotes within quotes -
in python 3.x:
print(""s"") # syntaxerror print("""s""") # prints s print(""""s"""") # syntaxerror print("""""s""""") # prints ""s
what reason behind different behaviour, when there different numbers of double quotes in string?
in python can create multiline strings """..."""
. quoting documentation strings,
string literals can span multiple lines. 1 way using triple-quotes:
"""..."""
or'''...'''
.
in first case, ""s""
parsed this
"" (empty string literal) s "" (empty string literal)
now, python doesn't know s
. why failing syntaxerror
.
in third case, string parsed this
""" "s """ (end of multiline string) `"`
now last "
has no matching "
. why failing.
in last case, """""s"""""
parsed this
""" ""s """ ""
so, multiline string parsed , have empty string literal next it. in python, can concatenate 2 string literals, writing them next each other, this
print ("a" "b") # ab
so, last empty string literal concatenated ""s
.
Comments
Post a Comment