Quoting quotes

William Lohrmann willia2501 at gmail.com
Fri Feb 26 17:04:55 CET 2010


On 26 Feb, 13:29, candide <cand... at 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"'



More information about the Python-list mailing list