我正在尝试将字符串中的标点符号替换为空格。我搜索了答案并在我的Python 2.7中尝试了它们,它们显示不同的结果。
s1=" merry's home, see a sign 'the-shop $on sale$ **go go!'" #sample string
print s1.translate(string.maketrans("",""), string.punctuation) #way1
print s1.translate(None,string.punctuation) #way2
table=string.maketrans(string.punctuation,' '*len(string.punctuation))
print s1.translate(table) #way3
它的输出结果如下:
merrys home see a sign theshop on sale go go
merrys home see a sign theshop on sale go go
merry s home see a sign the shop on sale go go
那么这些方法之间有什么区别呢?