Quoting quotes

Discussion in 'Python' started by candide, Feb 26, 2010.

  1. candide

    candide Guest

    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 ?
    candide, Feb 26, 2010
    #1
    1. Advertising

  2. candide

    Ivan Šipoš Guest

    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.
    Ivan Šipoš, Feb 26, 2010
    #2
    1. Advertising

  3. 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/
    Victor Stinner, Feb 26, 2010
    #3
  4. 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
    Steven D'Aprano, Feb 26, 2010
    #4
  5. 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!
    Grant Edwards, Feb 26, 2010
    #5
  6. On 26 Feb, 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 ?


    The best thing would be to backslash the single quote: print 'The play
    "All\'s Well That Ends Well"'
    William Lohrmann, Feb 26, 2010
    #6
  7. In message
    <>, 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
    Lawrence D'Oliveiro, Feb 26, 2010
    #7
  8. candide

    candide Guest

    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.
    candide, Feb 28, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris White

    Quotes/Double Quotes in Image Control

    Chris White, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,843
    Hermit Dave
    Sep 22, 2004
  2. Chris
    Replies:
    1
    Views:
    13,627
    Oisin
    Mar 24, 2006
  3. Lawrence Tierney

    Multiline quotes - escaping quotes - et al

    Lawrence Tierney, Dec 24, 2003, in forum: Java
    Replies:
    3
    Views:
    4,488
    Andrew Thompson
    Dec 24, 2003
  4. jsnX

    quoting quotes, &c.

    jsnX, Apr 5, 2005, in forum: C++
    Replies:
    1
    Views:
    352
    Victor Bazarov
    Apr 5, 2005
  5. jOhn
    Replies:
    1
    Views:
    219
    Phlip
    Jan 29, 2008
Loading...

Share This Page