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

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

    
    # Write a Python program that accepts a string and calculate the number of digits 
    # andletters.
    
        stre =input("enter the string-->")
        countl = 0
        countn = 0
        counto = 0
        for i in stre:
            if i.isalpha():
                countl += 1
            elif i.isdigit():
                countn += 1
            else:
                counto += 1
        print("The number of letters are --", countl)
        print("The number of numbers are --", countn)
        print("The number of characters are --", counto)
    

提交回复
热议问题