Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38506
| References | <CAGK9Mo9tO2zDS9JXpk7=LH7iEO0SY9sNMuvSF=0e_Yo-0SdyrA@mail.gmail.com> |
|---|---|
| Date | 2013-02-09 22:21 +1100 |
| Subject | Re: string.replace doesn't removes ":" |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1539.1360408910.2939.python-list@python.org> (permalink) |
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
Re: string.replace doesn't removes ":" Chris Angelico <rosuav@gmail.com> - 2013-02-09 22:21 +1100
csiph-web