Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61285

Re: Is It Bug?

From Peter Otten <__peter__@web.de>
Subject Re: Is It Bug?
Date 2013-12-08 11:43 +0100
Organization None
References <27c0b454-62e9-410f-b05c-7c5fe306f8aa@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.3723.1386499439.18130.python-list@python.org> (permalink)

Show all headers | View raw


Mahan Marwat wrote:

> Why this is not working.
> 
>>>> 'Hello, \\\\World'.replace('\\', '\\')
> 
> To me, Python will interpret '\\\\' to '\\'. And the replace method will
> replace '\\' with '\'. So, the result will be 'Hello, \World'. But it's
> give me 'Hello, \\\\World'.
> 
> The result I want form the code is 'Hello, \World'.

Let's forget about backslashes for the moment and use 'a' instead. We can 
replace an 'a' with an 'a'

>>> "Hello, aaWorld".replace("a", "a")
'Hello, aaWorld'

That changes nothing. Or we can replace two 'a's with one 'a'

>>> "Hello, aaWorld".replace("aa", "a")
'Hello, aWorld'

This does the obvious thing. Finally we can replace an 'a' with the empty 
string '':

>>> "Hello, aaWorld".replace("a", "")
'Hello, World'

This effectively removes all 'a's. 

Now let's replace the "a" with a backslash. Because the backslash has a 
special meaning it has to be "escaped", i. e. preceded by another backslash. 
The examples then become

>>> "Hello, \\\\World".replace("\\", "\\")
'Hello, \\\\World'
>>> "Hello, \\\\World".replace("\\\\", "\\")
'Hello, \\World'
>>> "Hello, \\\\World".replace("\\", "")
'Hello, World'

While doubling of backslashes is required by Python the doubling of 
backslahses in the output occurs because the interactive interpreter applies 
repr() to the string before it is shown. You can avoid that with an explicit 
print statement in Python 2 or a print() function call in Python 3:

>>> print "Hello, \\\\World".replace("\\", "\\")
Hello, \\World
>>> print "Hello, \\\\World".replace("\\\\", "\\")
Hello, \World
>>> print "Hello, \\\\World".replace("\\", "")
Hello, World                                                                                                                               

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Is It Bug? Mahan Marwat <mahanmarwat@gmail.com> - 2013-12-07 16:59 -0800
  Re: Is It Bug? Chris Angelico <rosuav@gmail.com> - 2013-12-08 12:05 +1100
    Re: Is It Bug? Roy Smith <roy@panix.com> - 2013-12-07 20:22 -0500
      Re: Is It Bug? Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-12-08 11:01 +0100
      Re: Is It Bug? Chris Angelico <rosuav@gmail.com> - 2013-12-08 21:04 +1100
      Re: Is It Bug? Chris Angelico <rosuav@gmail.com> - 2013-12-08 21:26 +1100
  Re: Is It Bug? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-08 01:06 +0000
  Re: Is It Bug? MRAB <python@mrabarnett.plus.com> - 2013-12-08 01:12 +0000
  Re: Is It Bug? Tim Roberts <timr@probo.com> - 2013-12-07 22:53 -0800
  Re: Is It Bug? Peter Otten <__peter__@web.de> - 2013-12-08 11:43 +0100

csiph-web