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


Groups > comp.lang.python > #38506

Re: string.replace doesn't removes ":"

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.024
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'subject:skip:s 10': 0.05; 'python': 0.09; 'bug': 0.10; '"this': 0.13; 'sat,': 0.15; '!!!!': 0.16; "'this": 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'replaces': 0.16; 'wrote:': 0.17; 'feb': 0.19; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'colon': 0.29; 'strings,': 0.29; 'probably': 0.29; 'print': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'text': 0.34; 'wrong': 0.34; 'doing': 0.35; 'pm,': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'little': 0.39; "you'll": 0.62; 'different': 0.63; '"");': 0.84; '2013': 0.84; 'difference.': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=0wBuYiouHu8jKsRf+WYkrIXDdo9plHzO3iOIorPF51M=; b=FJlf2Jf8M9Lo+Di5hs4/gDcaOyEeH5s9T/JSGOKs0/JrMVEfYYbyh0SlbUDpkSv4bY ikiUQfHap/fQsoezc7LhpYrHk/w3yRgscb33rEbWHw84yVz4eIZeU99B0JrCWUzmD6pS XBovLQl0mw+uZ37zIbo0H0/c2FX9kqGJrCRoV1ZVrNii0Wk5vbcXcceH8sRxvhVkusPQ LECrv+I/mVwYctBQyCcclztnsZ3WIcz2LsuQmoYbYwYjBLkel+SHeXZpNHJyNjt3/SI4 /Z8E9OHc3VyQGai01NhJ/i602doOwTT5oGp12q2BAF36gRemwDNg8HEjyh796bQxz9pZ 7luQ==
MIME-Version 1.0
X-Received by 10.221.11.205 with SMTP id pf13mr10515066vcb.70.1360408907678; Sat, 09 Feb 2013 03:21:47 -0800 (PST)
In-Reply-To <CAGK9Mo9tO2zDS9JXpk7=LH7iEO0SY9sNMuvSF=0e_Yo-0SdyrA@mail.gmail.com>
References <CAGK9Mo9tO2zDS9JXpk7=LH7iEO0SY9sNMuvSF=0e_Yo-0SdyrA@mail.gmail.com>
Date Sat, 9 Feb 2013 22:21:47 +1100
Subject Re: string.replace doesn't removes ":"
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1539.1360408910.2939.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360408910 news.xs4all.nl 6925 [2001:888:2000:d::a6]:34415
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38506

Show key headers only | View raw


On Sat, Feb 9, 2013 at 10:04 PM, Joshua Robinson
<shooki.robinson@gmail.com> wrote:
> Hi Monte-Pythons,
>
> x = "this is a simple : text: that has colon"
> s = x.replace(string.punctuation, "");  OR
> s = x.replace(string.punctuation, "");
> print x   # 'this is a simple : text: that has colon'
> # The colon is still in the text !!!!
>
> Is this a bug or am I doing something wrong ?

str.replace() replaces whole strings, not the individual characters.
You probably want str.translate():

s = x.translate(string.maketrans("",""),string.punctuation)

You'll then want to print s, rather than x, to see the difference.

Note that it's a little different in Python 3, and you would instead use:
s = x.translate(str.maketrans("","",string.punctuation))

ChrisA

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


Thread

Re: string.replace doesn't removes ":" Chris Angelico <rosuav@gmail.com> - 2013-02-09 22:21 +1100

csiph-web