Best way to strip punctuation from a string

前端 未结 26 1886
日久生厌
日久生厌 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 05:51

    I usually use something like this:

    >>> s = "string. With. Punctuation?" # Sample string
    >>> import string
    >>> for c in string.punctuation:
    ...     s= s.replace(c,"")
    ...
    >>> s
    'string With Punctuation'
    

提交回复
热议问题