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


Groups > comp.lang.python > #61285

Re: Is It Bug?

Path csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.05; 'output': 0.05; 'explicit': 0.07; 'removes': 0.07; 'skip:\\ 20': 0.07; 'string': 0.09; "'a'": 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'changes': 0.15; '"a"': 0.16; "'':": 0.16; "'\\\\'": 0.16; 'backslash': 0.16; 'backslashes': 0.16; 'preceded': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'repr()': 0.16; 'applies': 0.16; 'wrote:': 0.18; 'working.': 0.19; 'examples': 0.20; '>>>': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'instead.': 0.24; 'interpret': 0.24; 'replace': 0.24; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'statement': 0.30; 'code': 0.31; '"")': 0.31; '>>>>': 0.31; 'occurs': 0.31; 'another': 0.32; 'moment': 0.34; 'but': 0.35; 'method': 0.36; 'subject:?': 0.36; 'so,': 0.37; 'two': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'become': 0.64; 'finally': 0.65; 'effectively': 0.66; 'world': 0.66; 'obvious': 0.74; 'special': 0.74; 'doubling': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Is It Bug?
Date Sun, 08 Dec 2013 11:43:37 +0100
Organization None
References <27c0b454-62e9-410f-b05c-7c5fe306f8aa@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084b326.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3723.1386499439.18130.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1386499439 news.xs4all.nl 2903 [2001:888:2000:d::a6]:37911
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:61285

Show key headers only | 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