How can we strip punctuation at the start of a string using Python?

前端 未结 7 675
悲哀的现实
悲哀的现实 2021-01-14 11:43

I want to strip all kinds of punctuation at the start of the string using Python. My list contains strings and some of them starting with some kind of punct

相关标签:
7条回答
  • 2021-01-14 12:15

    You can use strip():

    Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed.

    Passing string.punctuation will remove all leading and trailing punctuation chars:

    >>> import string
    >>> string.punctuation
    '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
    
    >>> l = [',,gets', 'gets,,', ',,gets,,']
    >>> for item in l:
    ...     print item.strip(string.punctuation)
    ... 
    gets
    gets
    gets
    

    Or, lstrip() if you need only leading characters removed, rstip() - for trailing characters.

    Hope that helps.

    0 讨论(0)
提交回复
热议问题