Reputation: 33
I can add additional quotes to the beginning of a triple-quoted string, but not to the end. Why is that? This block of code:
print(""""
String that starts with quadruple quotes and ends with triple quotes
""")
Produces this output:
"
String that starts with quadruple quotes and ends with triple quotes
Yet this code block doesn't work:
print(""""
String that starts with quadruple quotes and ends with quadruple quotes
"""")
It produces this error:
File "example.py", line 3
"""")
^
SyntaxError: EOL while scanning string literal
I don't ever need to use a quadruple-quote string, but I'm curious why Python won't let me do it. Can anyone help me understand?
Upvotes: 3
Views: 4546
Reputation: 441
Unfortunately, instead of answering the question of why such a literal implementation with four trailing apostrophes, I was given an answer to the question ‘how to implement a literal using escaping’ or answered approximately like this - ‘because it is implemented that way’. It seems to me that I have found the answer. It is implemented this way due to another operation in Python - namely, concatenation of two literals without using the ‘+’ sign. For example, “F” “A” == “F”“A” == “FA”. Therefore, a literal with 8 apostrophes in a row “”“”“”“” could be interpreted differently. By the way, surprisingly, now 8" do not generate an error, but return an empty literal.
Upvotes: -1
Reputation: 184231
You get an error because you haven't closed a second set of quotes.
At the beginning of the string, you have four quotes. Three of these open the string. The next quote is included in the string, because there aren't another two quotes following it.
At the end of the string, you have four quotes. The first three of these end the string. The fourth opens a new single-quoted string. You don't close that string by the end of the line, so you get an error.
(It isn't an error, in fact, to have two string literals right next to them like that. Python will concatenate them at compile time.)
I don't ever need to use a quadruple-quote string, but I'm curious why Python won't let me do it.
Because quadruple-quoted strings aren't a thing. Python doesn't let you do it because it wasn't designed to do it. You can have single-quoted strings, and you can have triple-quoted strings. Any other number of quotes is right out.
Upvotes: 1
Reputation: 1122372
You can't use """
anywhere in the value of a triple-quoted string. Not at the start, and not at the end.
That's because, after the first three """
opening characters denoting the start of such a string, another sequence of """
is always going to be the end of the string. Your fourth "
lies outside of the string object you created, and a single "
without a closing "
is not a valid string.
Python has no other method of knowing when such a string ends. You can't arbitrarily extend the string 'inwards' with additional "
characters before the final """
, because that'd be indistinguishable from the valid and legal*:
>>> """string 1"""" string 2"
'string 1 string 2'
If you must include a "
before the closing """
, escape it. You can do so by preceding it with a backslash:
>>> """This is triple-quoted string that
... ends in a single double quote: \""""
'This is triple-quoted string that\nends in a single double quote: "'
Note that there is no such thing as a quadruple-quote string. Python doesn't let you combine "
quotes into longer sequences arbitrarily. Only "single quoted"
and """triple-quoted"""
syntax exists (using "
or '
). The rules for a triple-quoted string differ from a single-quoted string; newlines are allowed in the former, not in the latter.
See the String and Bytes literals section of the reference documentation for more details, which defines the grammar as:
shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"' longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
and explicitly mentions:
In triple-quoted literals, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the literal. (A “quote” is the character used to open the literal, i.e. either
'
or"
.)
(bold emphasis mine).
* The expression is legal because it consists of two string literals, one with """
quoting, the next with "
quoting. Consecutive string literals are automatically concatenated, just like they would in C. See String literal concatenation.
Upvotes: 9
Reputation: 3279
It's parsed as triple-quote text triple-quote quote
(triple-quote
first by the maximal munch rule). You can escape the first of the four quotes to fix this.
Upvotes: 3
Reputation: 281152
A triple-quoted string literal ends on a series of 3 unescaped matching quotation marks.
When a string literal starts with 4 quotes, the first 3 begin a triple-quoted string literal, and the next quote is just part of the string, since there would have to be three quotes in a row to end the string.
When you try to end a string literal with 4 quotes, the first three end the string literal and the 4th begins a new one. The new string literal doesn't have an ending quotation mark, making it a syntax error.
Upvotes: 1