Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; '(at': 0.03; 'string.': 0.04; 'subject:skip:s 10': 0.05; 'arguments': 0.07; 'python': 0.09; '[1]:': 0.09; 'dict': 0.09; 'method:': 0.09; 'to:addr:comp.lang.python': 0.09; 'tuple': 0.09; 'bug': 0.10; 'cc:addr:python-list': 0.10; 'programmer': 0.11; '"this': 0.13; '!!!!': 0.16; "'b',": 0.16; "'this": 0.16; 'bug,': 0.16; 'colons': 0.16; 'references:': 0.16; 'string:': 0.16; 'string': 0.17; 'wrote:': 0.17; 'char': 0.17; 'handles': 0.18; 'assuming': 0.22; 'sorry,': 0.22; 'cc:2**0': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'handling': 0.27; 'mind.': 0.27; 'replace': 0.27; 'colon': 0.29; "skip:' 10": 0.30; 'asking': 0.32; 'print': 0.32; '"")': 0.33; 'null': 0.33; 'handle': 0.33; 'received:google.com': 0.34; 'text': 0.34; 'wrong': 0.34; 'consistent': 0.35; 'doing': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'really': 0.36; 'but': 0.36; 'method': 0.36; 'should': 0.36; 'does': 0.37; 'why': 0.37; 'passed': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'list,': 0.39; 'skip:" 10': 0.40; 'think': 0.40; 'your': 0.60; 'most': 0.61; 'remove': 0.61; 'truly': 0.62; 'more': 0.63; 'wanting': 0.65; 'manner': 0.74; '"");': 0.84; '2013': 0.84 X-Received: by 10.49.95.68 with SMTP id di4mr1397099qeb.0.1360730649635; Tue, 12 Feb 2013 20:44:09 -0800 (PST) Newsgroups: comp.lang.python Date: Tue, 12 Feb 2013 20:44:09 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=70.196.64.56; posting-account=h3aEwQoAAACiuqX-oR3gvCVFm8lLHoWj References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 70.196.64.56 MIME-Version: 1.0 Subject: Re: string.replace doesn't removes ":" From: Rick Johnson To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360730657 news.xs4all.nl 6896 [2001:888:2000:d::a6]:41536 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38806 On Saturday, February 9, 2013 5:04:18 AM UTC-6, Joshua Robinson wrote: > Hi Monte-Pythons, >=20 > x =3D "this is a simple : text: that has colon" > s =3D x.replace(string.punctuation, ""); OR > s =3D x.replace(string.punctuation, "");=20 > print x # 'this is a simple : text: that has colon' >=20 > # The colon is still in the text !!!! >=20 > Is this a bug or am I doing something wrong ? Not a bug, you are just expecting Python to read your mind. In actuality wh= at you are doing is asking the method "string.replace()" to replace the 32 = char string: '!"#$%&\'()*+,-./:;<=3D>?@[\\]^_`{|}~'=20 with the null string:=20 "" ...what you INTENDED to do was have Python find every occurrence the each c= har in the string you passed and replace them with the null string. You are= assuming the replace method handles sequences in the manner you are projec= ting. Sorry, but string#replace does not work that way. Whether or not it s= hould is up for debate[1]. However, you can create a loop and pass the arguments "one-by-one" into the= method: py> s =3D "a:b :c: d" py> for char in string.punctuation: s =3D s.replace(char, "")=09 py> s 'ab c d' But this seems really wasteful if all you want is to remove colons. Are you= just removing the colons or truly wanting to remove ALL punctuation from t= he string? =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D REFERENCES: =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [1]: Should string.replace handle list, tuple and dict arguments in additio= n to strings? py> string.replace(('a', 'b', 'c'), 'abcdefgabc') 'defg' py> string.replace(['a', 'b', 'c'], 'abcdefgabc') 'defg' py> string.replace({'a':'A', 'b':'2', 'c':'C'}, 'abcdefgabc') 'A2CdefgA2C' This would be a more consistent approach to me. Handling only string argume= nts is woefully inadequate. Why would you have a programmer write a loop fo= r this every time?=20 @pydev&GvR What is the justification for not processing (at the minimum) multiple argu= ments? Do you think strings will most often only need one modification?