How to count digits, letters, spaces for a string in Python?

前端 未结 9 1414
忘了有多久
忘了有多久 2020-12-01 08:14

I am trying to make a function to detect how many digits, letter, spaces, and others for a string.

Here\'s what I have so far:

def count(x):
    leng         


        
9条回答
  •  悲哀的现实
    2020-12-01 08:42

    Following code replaces any nun-numeric character with '', allowing you to count number of such characters with function len.

    import re
    len(re.sub("[^0-9]", "", my_string))
    

    Alphabetical:

    import re
    len(re.sub("[^a-zA-Z]", "", my_string))
    

    More info - https://docs.python.org/3/library/re.html

提交回复
热议问题