Calculating Letters and Digits From The Sentence Using Python

Calculating Letters and Digits From The Sentence Using Python

To calculate the number of digits and the number of letters from a sentence using the python, we use python built in methods.

isalpha(): it is a built-in method in python used for string handling. isalpha() checks whether the string consists of alphabetic characters or not. This method will returns true if all the characters in the string are alphabets and it returns false if there are no alphabets.

isdigit(): it is a built-in method used for string handling. it checks whether the string consists of digits or numbers only. this method returns true if all the characters in the string are digits or numbers and it returns false if there are no digit

STEP 1: Create an input or assign an input to write the sentence from which we have to count the number of digits and letters.

STEP 2: create a dictionary with keys as letters, digits, and values as 0

STEP 3: using for loop and if loop check if the character in the string is letter append the number using isalpha() and if the character in the string is digit append the number using isdigit(),

STEP 4:  print the number of letters and digits using the print function.

sentence = raw_input()
dictionary ={"DIGITS":0, "LETTERS":0}
for character in sentence:
    if character.isdigit():
       dictionary["DIGITS"]+=1
    elif character.isalpha():
       dictionary["LETTERS"]+=1
    else:
        pass
print "LETTERS", dictionary["LETTERS"]
print "DIGITS", dictionary["DIGITS"]

 

OUTPUT:

INPUT = harikrishna1232123
LETTERS : 11
DIGITS  : 07

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *