Quoting quotes

C

candide

Suppose you have to put into a Python string the following sentence :

The play "All's Well That Ends Well" by Shakespeare

It's easy do it :
The play "All's Well That Ends Well" by Shakespeare

Now, change the sentence to this one :

The play "All's Well That Ends Well"

Using triple single quotes works fine
The play "All's Well That Ends Well"


But the first method doesn't run correctly :

File "<stdin>", line 1
print """The play "All's Well That Ends Well""""
^
SyntaxError: EOL while scanning single-quoted string

Any comment ?
 
I

Ivan Šipoš

Suppose you have to put into a Python string the following sentence :

The play "All's Well That Ends Well" by Shakespeare

It's easy do it :


The play "All's Well That Ends Well" by Shakespeare

Now, change the sentence to this one :

The play "All's Well That Ends Well"

Using triple single quotes works fine


The play "All's Well That Ends Well"


But the first method doesn't run correctly :



File "<stdin>", line 1
print """The play "All's Well That Ends Well""""
^
SyntaxError: EOL while scanning single-quoted string


Any comment ?
Well, there's no such thing as """" defined in python.
 
V

Victor Stinner

Le vendredi 26 février 2010 13:29:04, candide a écrit :
But the first method doesn't run correctly :

File "<stdin>", line 1
print """The play "All's Well That Ends Well""""
^
SyntaxError: EOL while scanning single-quoted string

Use triple simple single quotes:
"All's Well That Ends Well"
 
S

Steven D'Aprano

But the first method doesn't run correctly :


File "<stdin>", line 1
print """The play "All's Well That Ends Well""""
^
SyntaxError: EOL while scanning single-quoted string

Any comment ?

Of course not. Quotes can't be nested, so the first time the parser hits
three quote marks, you have reached the end of the string. You then open
a new string with a single quote mark, and then fail to close it. Hence
the EOL while scanning a single-quoted string.

There are many solutions. Here are four:
The play "All's Well That Ends Well"
 
G

Grant Edwards

Of course not. Quotes can't be nested, so the first time the
parser hits three quote marks, you have reached the end of the
string. You then open a new string with a single quote mark,
and then fail to close it. Hence the EOL while scanning a
single-quoted string.

IMO, the error message is misleading to many people, since in
many/most contexts the term "single-quoted" refers to this:

'here is a single quoted string'

And not this:

"this is a double-quoted string"
 
W

William Lohrmann

Suppose you have to put into a Python string the following sentence :

The play "All's Well That Ends Well" by Shakespeare

It's easy do it :


The play "All's Well That Ends Well" by Shakespeare

Now, change the sentence to this one :

The play "All's Well That Ends Well"

Using triple single quotes works fine


The play "All's Well That Ends Well"

But the first method doesn't run correctly :


  File "<stdin>", line 1
    print """The play "All's Well That Ends Well""""
                                                   ^
SyntaxError: EOL while scanning single-quoted string



Any comment ?

The best thing would be to backslash the single quote: print 'The play
"All\'s Well That Ends Well"'
 
L

Lawrence D'Oliveiro

In message
William said:
The best thing would be to backslash the single quote: print 'The play
"All\'s Well That Ends Well"'

Backslash-type escapes are the most general solution to this type of
problem. They’re also the easiest to apply automatically:

for ch in input_string :
if ch in troublesome_lot :
output backslash + tame representation of ch
else :
output ch
#end if
#end for
 
C

candide

OK, now I see the point. I was mistaken because I was supposing that
every quote strictly _inside_ the string have to match another quote od
the same type.

Thanks to all for yours responses.
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top