Python program to calculate the number of digits and letters in a string

29 Jun, 2022
Prerequisites: isnumeric() method – Python
Given a string, containing digits and letters, the task is to write a Python program to calculate the number of digits and letters in a string. The following block expresses the basic idea behind it:
Input: string = "geeks2for3geeks"
Output: total digits = 2 and total letters = 13

Input: string = "python1234"
Output: total digits = 4 and total letters = 6

Input: string = "co2mpu1te10rs"
Output: total digits = 4 and total letters = 9
The idea here is to solve this problem by iterating through all characters and check whether character is in all_digits or all_letters. 
Program:
Python3
# define all digits as stringall_digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] # define all lettersall_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',               'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] # given stringstring = "geeks2for3geeks" # initialized valuetotal_digits = 0total_letters = 0 # iterate through all charactersfor s in string:     # if character found in all_digits then increment total_digits by one    if s in all_digits:        total_digits += 1     # if character found in all_letters then increment total_letters by one    elif s in all_letters:        total_letters += 1 print("Total letters found :-", total_letters)print("Total digits found :-", total_digits)
Output:
Total letters found :- 13
Total digits found :- 2
Instead of checking character in all_letters, we can check:  
if character is found in all digits, then increment total_digits value by one
If not it means characters is a letter, increment total_letters value by one
 
Program:
 
Python3
# define all digits as stringall_digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] # define all lettersall_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',               'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] # given stringstring = "geeks2for3geeks" # initialized valuetotal_digits = 0total_letters = 0 # iterate through all charactersfor s in string:     # if character found in all_digits then increment total_digits by one    if s in all_digits:        total_digits += 1     # if character not found in all_digits then increment total_letters by one    else:        total_letters += 1 print("Total letters found :-", total_letters)print("Total digits found :-", total_digits)
Output:
Total letters found :- 13
Total digits found :- 2
The idea here is to solve this problem by iterating through all characters and check whether character is letter or digits using isnumeric() function. If isnumeric is True, it means character is digit, else character is a letter.
Program:
Python3
# given stringstring = "python1234" # initialized valuetotal_digits = 0total_letters = 0 # iterate through all charactersfor s in string:     # if character is digit (return True)    if s.isnumeric():        total_digits += 1     # if character is letter (return False)    else:        total_letters += 1 print("Total letters found :-", total_letters)print("Total digits found :-", total_digits)
Output:
Total letters found :- 6
Total digits found :- 4
Shorter Approach#: Using re.findall() and len function
Here we use these function which make our solution more easy. First we find all the digits in string with the help of re.findall() which give list of matched pattern with the help of len we calculate the length of list and similarly we find the total letters in string with the help of re.findall() method and calculate the length of list using len. 
Python3
import re# given stringstring = "geeks2for3geeks" # initialized valuetotal_digits = len(re.findall('[0-9]',string))total_letters = len(re.findall('[A-z]', string)) # iterate through all charactersprint("Total letters found :-", total_letters)print("Total digits found :-", total_digits)
Output:
Total letters found :- 13
Total digits found :- 2
ruhelaa48
satyam00so
Python string-programs
Technical Scripter 2020
Python
Python Programs
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary
Python Program for Fibonacci numbers