[Tutor] Escaping a single quote mark in a triple quoted string.

Ray Parrish crp at cmc.net
Sat Mar 13 19:13:37 CET 2010


Sander Sweers wrote:
> On 13 March 2010 18:33, Ray Parrish <crp at cmc.net> wrote:
>   
>> Hello,
>>
>> I am getting the following -
>>
>>     
>>>>> String = """<a href="http://www.rayslinks.com">Ray's Links</a>"""
>>>>> String
>>>>>           
>> '<a href="http://www.rayslinks.com">Ray\'s Links</a>'
>>
>> Note the magically appearing back slash in my result string.
>>     
>
> It is not really there. When you have a single quote in a single quote
> string it need to be escaped with a backslash. Simulary a double quote
> in a double quote string. The triple quote string does this for you.
>
> When you write the string to a file the backslash will "magically" disappear.
>
>   
>> Now I tiry to escape the single quote.
>>
>>     
>>>>> String = """<a href="http://www.rayslinks.com">Ray\'s Links</a>"""
>>>>> String
>>>>>           
>> '<a href="http://www.rayslinks.com">Ray\'s Links</a>'
>>
>> Once again the unwanted back slash appears.
>>     
>
> See above single quotes in single quote string need to be escaped.
>
>   
>>>>> NewString = """'"""
>>>>> NewString
>>>>>           
>> "'"
>> Hmmm, no back slash this time...
>>     
>
> Correct, here you have a single quote in a double quote string.
>
>   
>>>>> String = """<a href="http://www.rayslinks.com">Ray""" + """'""" + """s
>>>>> Links</a>"""
>>>>> String
>>>>>           
>> '<a href="http://www.rayslinks.com">Ray\'s Links</a>'
>>
>> Why did quoting the single quote work in NewString when I triple quoted just
>> the single quote, but not in the other examples where the result shows a
>> back slash before my singe quote in String?
>>
>> Is there a proper way to do this?
>>     
>
> You are trying to solve something that is not really a problem. What
> is however is your usage of a single quote in html text. You need to
> replace it with &#39; or &lsquo;.
>
> Greets
> Sander
>   
Thanks for your quick answer. I'll accept that there will not be a back 
slash in my file when I write it then.

I was not aware that single quotes were bad news in HTML. I don't think 
I've ever experienced a problem using them myself, but will adjust, and 
start using the & code for it instead.

Later, Ray Parrish

-- 
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com




More information about the Tutor mailing list