Python doesn't recognize quote types

W

wxPythoner

There's a thing that bugs me in Python. Look at this...
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?
 
N

Nicolas Dandrimont

There's a thing that bugs me in Python. Look at this...

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?

The "single-quoted string" (e.g. 'foo' or "bar") is so named by
opposition to triple-quoted (e.g. '''foo''' or """bar""") strings.

Regards,
--
Nicolas Dandrimont


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFIJhBbacIxuZqlam0RAkiSAJ45YCIhnsSrOlK4ZEijkYY3kwz4xQCfazat
Y7eHbwTFb1coeCJ6MBSJw64=
=Oz3f
-----END PGP SIGNATURE-----
 
J

John Machin

There's a thing that bugs me in Python. Look at this...


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:

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
 
M

MRAB

True, but that problem is outside of the Python community's control. Given
that people do often refer to single quote when they mean apostrophe the
error message should be written so as to minimise confusion.

FYI, there are actually 2 types of quote character, single and double,
each with 2 forms, left and right. In Unicode the single-quote
characters are U+2018 (‘) and U+2019 (’) and the double-quote
characters are U+201C (“) and U+201D (”). The right-hand single-quote
is also the apostrophe.

In order to reduce the size of the character set needed, computer
manufacturers introduced 'sexless' quote characters, the single-quote/
aphostrophe U+0027 (') and double-quote U+0022 (").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top