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

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

    INPUT :

    1

    26

    sadw96aeafae4awdw2 wd100awd

    import re
    
    a=int(input())
    for i in range(a):
        b=int(input())
        c=input()
    
        w=re.findall(r'\d',c)
        x=re.findall(r'\d+',c)
        y=re.findall(r'\s+',c)
        z=re.findall(r'.',c)
        print(len(x))
        print(len(y))
        print(len(z)-len(y)-len(w))
    

    OUTPUT :

    4

    1

    19

    The four digits are 96, 4, 2, 100 The number of spaces = 1 number of letters = 19

提交回复
热议问题