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

前端 未结 7 735
悲哀的现实
悲哀的现实 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:09

    To remove punctuation, spaces, numbers from the beginning of each string in a list of strings:

    import string
    
    chars = string.punctuation + string.whitespace + string.digits    
    a[:] = [s.lstrip(chars) for s in a]
    

    Note: it doesn't take into account non-ascii punctuation, whitespace, or digits.

提交回复
热议问题