[python] How to remove all the punctuation in a string? (Python)

For example:

asking="hello! what's your name?"

Can I just do this?

asking.strip("!'?")

This question is related to python

The answer is


This works, but there might be better solutions.

asking="hello! what's your name?"
asking = ''.join([c for c in asking if c not in ('!', '?')])
print asking

import string

asking = "".join(l for l in asking if l not in string.punctuation)

filter with string.punctuation.


Strip won't work. It only removes leading and trailing instances, not everything in between: http://docs.python.org/2/library/stdtypes.html#str.strip

Having fun with filter:

import string
asking = "hello! what's your name?"
predicate = lambda x:x not in string.punctuation
filter(predicate, asking)