In this video you will will learn how to Calculate the Number of Digits and Letters in a String (See the source further down)
Python Program to Calculate the Number of Digits and Letters in a String
==Source Code==
string=input("Enter string:")
count1=0
count2=0
for i in string:
if(i.isdigit()):
count1=count1+1
count2=count2+1
print("The number of digits is:")
print(count1)
print("The number of characters is:")
print(count2)
==EndCode==
Program Explanation
1. User must enter a string and store it in a variable.
2. Both the count variables are initialized to zero.
3. The for loop is used to traverse through the characters in the string.
4. The first count variable is incremented each time a digit is encountered and the second count variable is incremented each time a character is encountered.
5. The total count of digits and characters in the string are printed.