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

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

    sample = ("Python 3.2 is very easy") #sample string  
    letters = 0  # initiating the count of letters to 0
    numeric = 0  # initiating the count of numbers to 0
    
            for i in sample:  
                if i.isdigit():      
                    numeric +=1      
                elif i.isalpha():    
                    letters +=1    
                else:    
                   pass  
    letters  
    numeric  
    

提交回复
热议问题