1. 程式人生 > >python一次去掉所有標點符號

python一次去掉所有標點符號

既然是一次性去掉所有的標點符號,那當然是用正則表示式啦:

import re
 
punctuation = '!,;:?"\''
def removePunctuation(text):
    text = re.sub(r'[{}]+'.format(punctuation),'',text)
    return text.strip().lower()
 
text = " Hello, world!  "
print removePunctuation(text)