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

前端 未结 9 1375
忘了有多久
忘了有多久 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:46

    def match_string(words):
        nums = 0
        letter = 0
        other = 0
        for i in words :
            if i.isalpha():
                letter+=1
            elif i.isdigit():
                nums+=1
            else:
                other+=1
        return nums,letter,other
    
    x = match_string("Hello World")
    print(x)
    >>>
    (0, 10, 2)
    >>>
    

提交回复
热议问题