如何在Python中从字符串中删除所有标点符号?
删除字符串中所有标点符号的最快方法是使用str.translate()。您可以按以下方式使用它:
import string s = "string. With. Punctuation?" print s.translate(None, string.punctuation)
这将给我们输出:
string With Punctuation
如果您想要一个更具可读性的解决方案,则可以显式地遍历集合,并在循环中忽略所有标点,如下所示:
import string s = "string. With. Punctuation?" exclude = set(string.punctuation) s = ''.join(ch for ch in s if ch not in exclude) print s
这将给我们输出:
string With Punctuation
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短