[Tutor] Escaping double quotes

w chun wescpy at gmail.com
Thu Dec 8 08:08:56 CET 2005


> > >>> label = 'this is "quoted"'
> > >>> label.replace('"','\"')
> > 'this is "quoted"'
> > ## This works
> > >>> label.replace('"','\'')
> > "this is 'quoted'"
>
>   <blush>
>   What I should have been using is label.replace('"','\\"')
>   :-) Nevermind.


hold on a second pardner! :-)  what were you trying to do, provide
escaped quotes?  because if it was something else, like replacing the
double quotes with singles, you had it correct.

otherwise your solution can also be done with raw strings:

>>> label.replace('"','\\"')
'this is \\"quoted\\"'
>>> label.replace('"',r'\"')
'this is \\"quoted\\"'

ok, i'll stop thinking about it now.  ;-)

cheers,
-wesley


More information about the Tutor mailing list