Issue
I've been studying the Python Language Reference carefully, for some Anki flashcards I'm writing. And it appears I've hit a snag while going over string literals.
See here: The function call print("can't")
returns can't
.
But I've been unable to find anything in the docs that suggests this is even possible!
Consider this excerpt of the formal definition:
shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"'
longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
shortstringitem ::= shortstringchar | stringescapeseq
longstringitem ::= longstringchar | stringescapeseq
shortstringchar ::= <any source character except "\" or newline or the quote>
longstringchar ::= <any source character except "\">
stringescapeseq ::= "\" <any source character>`
As you may be able to tell, the sequence "can't"
is a short-string. That's because it is a group of zero or more short-string items surrounded by matching groups of double quotes.
And short-string items may either be short-string characters or string escape sequences.
Since there are no string escape sequences in the sequence "can't"
, let's look at short-string characters. The definition says that the quote (') is not a short-string character.
Since the quote is not a short-string character, it makes sense that print('can't')
throws a syntax error. Why doesn't print("can't")
follow the same rules?
I feel like the Python Language Reference has made an error of omission here. Thoughts?
Solution
Since "can't"
starts with a double quote, you're allowed to have a single quote in the literal.
shortstringchar ::= <any source character except "\" or newline or the quote>
By "the quote" the specification means the character (either ' or ") that opened the string literal. This allows you to have either single or double quotes in a string literal (without escaping) by enclosing the literal in the opposite quote characters.
Answered By - Bill the Lizard
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.