<br><br><div class="gmail_quote">On Thu, Feb 5, 2009 at 5:59 PM, <span dir="ltr"><<a href="mailto:rdmurray@bitdance.com">rdmurray@bitdance.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d">"S.Selvam Siva" <<a href="mailto:s.selvamsiva@gmail.com">s.selvamsiva@gmail.com</a>> wrote:<br>
</div><div class="Ih2E3d">> I tried to do a string replace as follows,<br>
><br>
> >>> s="hi & people"<br>
> >>> s.replace("&","\&")<br>
> 'hi \\& people'<br>
> >>><br>
><br>
> but i was expecting 'hi \& people'.I dont know ,what is something different<br>
> here with escape sequence.<br>
<br>
</div>You are running into the difference between the 'repr' of a string (which<br>
is what is printed by default at the python prompt) and the actual<br>
contents of the string. In the repr the backslash needs to be escaped<br>
by prefixing it with a backslash, just as you would if you wanted to<br>
enter a backslash into a string in your program. If you print the string,<br>
you'll see there is only one backslash. Note that you didn't need to<br>
double the backslash in your replacement string only because it wasn't<br>
followed by a character that forms an escape...but the repr of that<br>
string will still have the backslash doubled, and that is really the<br>
way you should write it in your program to begin with for safety's sake.<br>
<br>
Python 2.6.1 (r261:67515, Jan 7 2009, 17:09:13)<br>
[GCC 4.3.2] on linux2<br>
Type "help", "copyright", "credits" or "license" for more information.<br>
>>> s="hi & people"<br>
>>> replacementstring = "\&"<br>
>>> replacementstring<br>
'\\&'<br>
>>> print replacementstring<br>
\&<br>
>>> x = s.replace("&","\\&")<br>
>>> x<br>
'hi \\& people'<br>
>>> print x<br>
hi \& people<br>
<font color="#888888"><br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a></font></blockquote><div><br><br>Thank you all for your response,<br><br>Now i understood the way python terminal expose '\'. <br>
</div></div><br><br clear="all"><br>-- <br>Yours,<br>S.Selvam<br>