Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: string replace for back slash (http://www.velocityreviews.com/forums/t668982-re-string-replace-for-back-slash.html)

Chris Rebert 02-05-2009 11:59 AM

Re: string replace for back slash
 
On Thu, Feb 5, 2009 at 3:40 AM, S.Selvam Siva <s.selvamsiva@gmail.com> wrote:
> Hi all,
>
> I tried to do a string replace as follows,
>
>>>> s="hi & people"
>>>> s.replace("&","\&")

> 'hi \\& people'
>>>>

>
> but i was expecting 'hi \& people'.I dont know ,what is something different
> here with escape sequence.


The Python interactive interpreter does an implicit repr() (consult
the docs if you're unfamiliar with the repr() function) on the return
value of the expression. Note the single quotes in the output, which
obviously wouldn't be present in the string itself; same thing with
the doubling of the backslash.
If you instead do `print s.replace("&","\&")`, you'll see that the
outputted string does indeed only contain 1 backslash.

It's a common newbie confuser, there really should be a FAQ about it;
pity there isn't.

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com


All times are GMT. The time now is 04:24 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.