Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38506 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2013-02-09 22:21 +1100 |
| Last post | 2013-02-09 22:21 +1100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: string.replace doesn't removes ":" Chris Angelico <rosuav@gmail.com> - 2013-02-09 22:21 +1100
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-02-09 22:21 +1100 |
| Subject | Re: string.replace doesn't removes ":" |
| Message-ID | <mailman.1539.1360408910.2939.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web