In Python 3.x:
print(""s"") # SyntaxError
print("""s""") # prints s
print(""""s"""") # SyntaxError
print("""""s""""") # prints ""s
What is the reason behind this different behaviour, when there are different numbers of double quotes in the string?
In Python you can create multiline strings with
"""..."""
. Quoting the documentation for strings,In your first case,
""s""
is parsed like thisNow, Python doesn't know what to do with
s
. That is why it is failing withSyntaxError
.In your third case, the string is parsed like this
Now the last
"
has no matching"
. That is why it is failing.In the last case,
"""""s"""""
is parsed like thisSo, the multiline string is parsed successfully and then you have an empty string literal next to it. In Python, you can concatenate two string literals, by writing them next to each other, like this
So, the last empty string literal is concatenated with the
""s
.