This is a Python Program to calculate the number of digits and letters in a string.
The program takes a string and calculates the number of digits and letters in a string.
1. Take a string from the user and store it in a variable.
2. Initialize the two count variables to 0.
3. Use a for loop to traverse through the characters in the string and increment the first count variable each time a digit is encountered and increment the second count variable each time a character is encountered.
4. Print the total count of both the variables.
5. Exit.
Here is source code of the Python Program to calculate the number of digits and letters in a string. The program output is also shown below.
string=raw_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)
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.
Case 1: Enter string:Hello123 The number of digits is: 3 The number of characters is: 8 Case 2: Enter string:Abc12 The number of digits is: 2 The number of characters is: 5
Sanfoundry Global Education & Learning Series – 1000 Python Programs.
If you wish to look at all Python Programming examples, go to 1000 Python Programs.
- Check Information Technology Books
- Check Python Books
- Apply for Programming Internship
- Practice Programming MCQs
- Apply for Python Internship