[Python-Dev] this python string literals documentation couldn't explain me: single quote presence inside double quoted string and viceversa. Can Anyone explain me?

Steven D'Aprano steve at pearwood.info
Wed May 8 14:16:14 CEST 2013


On 08/05/13 21:31, Alok Nayak wrote:
> I asked this question here,
> http://stackoverflow.com/questions/16435233/this-python-string-literals-documentation-couldnt-explain-me-single-quote-pres,
> . I was advised to ask here


They were wrong. It is not relevant here, since it is not a question about development of Python. But I will answer your question anyway.

The relevant parts of the documentation are:

shortstring     ::=  "'" shortstringitem* "'" | '"' shortstringitem* '"'
shortstringitem ::=  shortstringchar | escapeseq
shortstringchar ::=  <any source character except "\" or newline or the quote>

So let's look at a string:

'a"b'

This is a shortstring, made up of single-quote followed by three shortstringitems, followed by single-quote. All three shortstring items are shortstringchar, not escapeseq:

a is a source character, not including "\" or newline or single-quote
" is a source character, not including "\" or newline or single-quote
b is a source character, not including "\" or newline or single-quote


[...]
>> shortstringchar ::=  <any source character except "\" or newline or the quote>
>>
>>   here in this definition 'the quote' isn't specific whether single (') or
>> double (").

Correct. You are expected to understand that it means either single-quote or double-quote according to context.

This is documentation aimed at a human reader who should be able to use human reasoning skills to understand what "the quote" means, it is not the literal BNF grammar used by the compiler to compile Python's parser. For brevity and simplicity, some definitions may be simplified.



-- 
Steven


More information about the Python-Dev mailing list