Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Quoting quotes

Reply
Thread Tools

Quoting quotes

 
 
candide
Guest
Posts: n/a
 
      02-26-2010
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 :

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

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

>>> print '''The play "All's Well That Ends Well"'''

The play "All's Well That Ends Well"


But the first method doesn't run correctly :


>>> print """The play "All's Well That Ends Well""""

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



Any comment ?


 
Reply With Quote
 
 
 
 
Ivan Šipoš
Guest
Posts: n/a
 
      02-26-2010
On 26.2.2010. 13:29, candide wrote:
> 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 :
>
>
>>>> print """The play "All's Well That Ends Well" by Shakespeare"""
>>>>

> 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
>
>
>>>> print '''The play "All's Well That Ends Well"'''
>>>>

> The play "All's Well That Ends Well"
>
>
> But the first method doesn't run correctly :
>
>
>
>>>> print """The play "All's Well That Ends Well""""
>>>>

> 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.

 
Reply With Quote
 
 
 
 
Victor Stinner
Guest
Posts: n/a
 
      02-26-2010
Le vendredi 26 février 2010 13:29:04, candide a écrit :
> But the first method doesn't run correctly :
> >>> print """The play "All's Well That Ends Well""""

>
> 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:

>>> print '''"All's Well That Ends Well"'''

"All's Well That Ends Well"

--
Victor Stinner
http://www.haypocalc.com/
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-26-2010
On Fri, 26 Feb 2010 13:29:04 +0100, candide wrote:

> But the first method doesn't run correctly :
>
>
>>>> print """The play "All's Well That Ends Well""""

> 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:

>>> print """The play "All's Well That Ends Well\""""

The play "All's Well That Ends Well"
>>> print '''The play "All's Well That Ends Well"'''

The play "All's Well That Ends Well"
>>> print 'The play "All\'s Well That Ends Well"'

The play "All's Well That Ends Well"
>>> print 'The play "All' "'" 's Well That Ends Well"'

The play "All's Well That Ends Well"

--
Steven
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      02-26-2010
On 2010-02-26, Steven D'Aprano <> wrote:
> On Fri, 26 Feb 2010 13:29:04 +0100, candide wrote:
>
>> But the first method doesn't run correctly :
>>
>>
>>>>> print """The play "All's Well That Ends Well""""

>> 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.


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"

--
Grant Edwards grante Yow! And then we could sit
at on the hoods of cars at
visi.com stop lights!
 
Reply With Quote
 
William Lohrmann
Guest
Posts: n/a
 
      02-26-2010
On 26 Feb, 13:29, candide <cand...@free.invalid> wrote:
> 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 :
>
> >>> print """The play "All's Well That Ends Well" by Shakespeare"""

>
> 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
>
> >>> print '''The play "All's Well That Ends Well"'''

>
> The play "All's Well That Ends Well"
>
> But the first method doesn't run correctly :
>
> >>> print """The play "All's Well That Ends Well""""

>
> * 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"'
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      02-26-2010
In message
<bda4748c-49ec-4383-81c1->, William
Lohrmann wrote:

> 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
 
Reply With Quote
 
candide
Guest
Posts: n/a
 
      02-28-2010
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP double quotes inside double quotes MSB Computer Support 11 10-21-2006 01:09 PM
Asp.NET Javascript string, want to pass '(single quotes' within '(single quotes) Chris ASP .Net 1 03-24-2006 09:03 PM
quoting quotes, &c. jsnX C++ 1 04-05-2005 03:31 PM
Quotes/Double Quotes in Image Control Chris White ASP .Net 1 09-22-2004 06:22 AM
Multiline quotes - escaping quotes - et al Lawrence Tierney Java 3 12-24-2003 05:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57