Velocity Reviews

Velocity Reviews (
-   Python (
-   -   Re: string replace for back slash (

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 <> 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.


Follow the path of the Iguana...

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.