string.replace doesn't removes ":"
rosuav at gmail.com
Sat Feb 9 12:21:47 CET 2013
On Sat, Feb 9, 2013 at 10:04 PM, Joshua Robinson
<shooki.robinson at 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))
More information about the Python-list