Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: string replace for back slash

Thread Tools

Re: string replace for back slash

Chris Rebert
Posts: n/a
On Thu, Feb 5, 2009 at 3:40 AM, S.Selvam Siva <(E-Mail Removed)> 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...
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
string replace for back slash Python 0 02-05-2009 12:29 PM
removing back slash character from a string andrew ASP .Net 7 10-09-2008 07:06 PM
string translate, replace, find and the forward slash destroooooy Python 4 04-29-2008 09:46 PM
replace double inverted commas into slash double inverted commas in string AviraM Java 2 09-28-2006 06:19 PM
Javascript Functions Back Slash! Javascript 4 06-26-2004 05:10 PM