python3: removing several chars from a string with a long chain of .replace().replace().replace()

Posted by MadSc13ntist on Stack Overflow See other posts from Stack Overflow or by MadSc13ntist
Published on 2010-06-02T14:40:13Z Indexed on 2010/06/02 14:44 UTC
Read the original article Hit count: 482

Filed under:
|
|

I found this example on stack overflow. I understand it, but seems like a bit much for such a simple method concept... removing several chars from a string.

import string
exclude = set(string.punctuation)
s = ''.join(ch for ch in s if ch not in exclude)

is there a builtin string method in python 3.1 to do something to the tune of:

s = "a,b,c,d,e,f,g,h,i"
s = s.strip([",", "d", "h"])

instead of:

s = s.replace(",", "").replace("d", "").replace("h", "")

© Stack Overflow or respective owner

Related posts about string

Related posts about method