Best way to strip punctuation from a string

前端 未结 26 1811
日久生厌
日久生厌 2020-11-21 05:39

It seems like there should be a simpler way than:

import string
s = \"string. With. Punctuation?\" # Sample string 
out = s.translate(string.maketrans(\"\",\         


        
26条回答
  •  北海茫月
    2020-11-21 06:03

    Not necessarily simpler, but a different way, if you are more familiar with the re family.

    import re, string
    s = "string. With. Punctuation?" # Sample string 
    out = re.sub('[%s]' % re.escape(string.punctuation), '', s)
    

提交回复
热议问题