Best way to strip punctuation from a string

前端 未结 26 1826
日久生厌
日久生厌 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:16

    I like to use a function like this:

    def scrub(abc):
        while abc[-1] is in list(string.punctuation):
            abc=abc[:-1]
        while abc[0] is in list(string.punctuation):
            abc=abc[1:]
        return abc
    

提交回复
热议问题