Python doesn't recognize quote types

John Machin sjmachin at lexicon.net
Sat May 10 17:59:12 EDT 2008


On May 11, 6:56 am, wxPytho... at gmail.com wrote:
> There's a thing that bugs me in Python. Look at this...
>
> >>> print "Testing\"
>
> SyntaxError: EOL while scanning single-quoted string
>
> Please focus on the part of the error message that states "while
> scanning single-quoted string". How can Python claim it scanned a
> single-quoted string when I fed it with a double-quoted string? Is
> quote type (single quote and double quote) recognition not implemented
> in Python?

Read this:
http://docs.python.org/ref/strings.html

Try each of these:
print 'Testing
print 'Testing\'
print 'Testing\'Testing
print 'Testing'
print 'Testing\''
print 'Testing\'Testing'

There's a wrinkle that's common to both your questions: \" causes the
" not to be regarded as (part of) the end marker but to be included as
a data character. Similarly with '. Examples:

>>> print "She said \"Hello!\""
She said "Hello!"
>>> print 'His surname is "O\'Brien"'
His surname is "O'Brien"
>>>

In the error message, "quoted" is the past tense of the verb "to
quote", meaning to wrap a string of characters with a leading string
and a trailing string to mark the contained string as a lexical item,
typically a string constant. The message is intended to convey that
the leading marker has been seen, but an EOL (end of line) was reached
without seeing the trailing marker.

A better error message might be something like "String constant not
terminated at end of line".

Unfortunately the above-mentioned documentation uses xxxxle-quote as a
noun to describe characters -- IMHO this is colloquial and confusing;
it should call ' an apostrophe, not a "single-quote", and all " a
quote, not a "double-quote". The confusion is compounded by referring
to '''abc''' and """xyz""" as triple-quoted strings ... so one might
expect 'abc' and "xyz" to be called "single-quoted strings", and this
sense is what is being used in the error message.

HTH,
John





More information about the Python-list mailing list