Question:
Write a program that accepts a sentence and calculate the number of letters and digits.
Program:
1 2 3 4 5 6 7 8 9 10 11 |
st = raw_input("Enter string:\n") d={"DIGITS":0, "LETTERS":0} for c in st: if c.isdigit(): d["DIGITS"]+=1 elif c.isalpha(): d["LETTERS"]+=1 else: pass print "LETTERS", d["LETTERS"] print "DIGITS", d["DIGITS"] |
Explanation:
Function isdigit() checks whether the concerned character is a digit, while isalpha checks whether it is an alphabet
Output:
1 2 3 4 |
Enter string: hai and hello! Welcome to the 1 and only GlobalSQA! LETTERS 39 DIGITS 1 |
Leave a Reply