punctuation

Best way to strip punctuation from a string

走远了吗. 提交于 2019-11-25 21:41:39
问题 It seems like there should be a simpler way than: import string s = \"string. With. Punctuation?\" # Sample string out = s.translate(string.maketrans(\"\",\"\"), string.punctuation) Is there? 回答1: From an efficiency perspective, you're not going to beat s.translate(None, string.punctuation) For higher versions of Python use the following code: s.translate(str.maketrans('', '', string.punctuation)) It's performing raw string operations in C with a lookup table - there's not much that will beat