Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 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

First Approach

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 string
all_digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
 
# define all letters
all_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 string
string = "geeks2for3geeks"
 
# initialized value
total_digits = 0
total_letters = 0
 
# iterate through all characters
for 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

Optimized Method

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 string
all_digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
 
# define all letters
all_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 string
string = "geeks2for3geeks"
 
# initialized value
total_digits = 0
total_letters = 0
 
# iterate through all characters
for 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

Second Approach (More Optimized Method)

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 string
string = "python1234"
 
# initialized value
total_digits = 0
total_letters = 0
 
# iterate through all characters
for 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 string
string = "geeks2for3geeks"
 
# initialized value
total_digits = len(re.findall('[0-9]',string))
total_letters = len(re.findall('[A-z]', string))
 
# iterate through all characters
print("Total letters found :-", total_letters)
print("Total digits found :-", total_digits)

Output:

Total letters found :- 13
Total digits found :- 2


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!