For example:
asking="hello! what's your name?"
Can I just do this?
asking.strip("!'?")
For example:
asking="hello! what's your name?"
Can I just do this?
asking.strip("!'?")
A really simple implementation is:
out = "".join(c for c in asking if c not in ('!','.',':'))
and keep adding any other types of punctuation.
A more efficient way would be
import string
stringIn = "string.with.punctuation!"
out = stringIn.translate(stringIn.maketrans("",""), string.punctuation)
Edit: There is some more discussion on efficiency and other implementations here: Best way to strip punctuation from a string in Python
import string
asking = "".join(l for l in asking if l not in string.punctuation)
filter with string.punctuation
.
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
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)