Saturday, December 28, 2024
Google search engine
HomeLanguagesPython program to calculate the number of digits and letters in a...

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

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

Example:

Input: string = "Lazyroar2for3Lazyroar"
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
 
Explanation: Here we are calculating the number of digits and alphabets in the given string.

Method 1: Using the built-in method isalpha()

Python3




alpha,string=0,"Geeks1234"
for i in string:
    if (i.isalpha()):
        alpha+=1
print("Number of Digit is", len(string)-alpha)
print("Number of Alphabets is", alpha)


Output:

Number of Digit is 4
Number of Alphabets is 5

Explanation:

Here we have used the built-in method isalpha() which generally helps us to identify whether that particular character is a alphabet or not and if it’s not then we simply ignore it. Assuming the condition that the string only constitutes of alphabets and digits then we can conclude that whether that character will be a digit or a alphabet. We already have the count of all the alphabets then we can subtract the count with the length of the string and hence we can get the number of digits.

Time complexity :  O(n)

Space complexity : O(1)

Method 2: Using all digit and all letter lists

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 = "Lazyroar2for3Lazyroar"
 
# 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

Explanation:

The idea here is to solve this problem by iterating through all characters and checking whether the character is in all_digits that store all the digits or all_letters that store all the alphabets in list

Time complexity : O(n)

Space complexity : O(1)

Method 3: By just checking one of the above conditions

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 = "Lazyroar2for3Lazyroar"
 
# 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

Explanation:

Instead of checking characters in all_letters, we can check:  

  • if the character is found in all digits, then increment the total_digits value by one
  • If not it means characters is a letter, increment total_letters value by one

Time complexity :  O(n)

Space complexity : O(1)

Method 3: By using the built in method isnumeric()

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

Explanation:

The idea here is to solve this problem by iterating through all characters and checking whether the character is a letter or digits using isnumeric() function. If isnumeric() is True, it means a character is a digit, else character is a letter.

Time Complexity : O(n)

Space Complexity : O(1)

Method 4: Using re.findall() and len() function

Python3




import re
# given string
string = "Lazyroar2for3Lazyroar"
 
# 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

Explanation:

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. 

The time and space complexity for all the methods are the same:

Time Complexity: O(n)

Auxiliary Space: O(n)

Method 5: Using ord() function

Python3




#Count letters and numbers in string
string = "Lazyroar2for3Lazyroar"
 
# 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 ord(s) in range(48,58):
        total_digits += 1
 
    # if character found in all_letters then increment total_letters by one
    elif ord(s) in range(65,91) or ord(s) in range(97,123):
        total_letters += 1
 
print("Total letters found :-", total_letters)
print("Total digits found :-", total_digits)


Output:

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

Explanation:

Here we are using the ord() function which returns the ascii code of the character and hence we compare it in their respective zones or range and get the count of digits and alphabets separately.

Time Complexity : O(n)

Space Complexity : O(1)

Method 6:  using operator.countOf() method

Python3




import operator as op
# 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 = "Lazyroar2for3Lazyroar"
 
# 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 op.countOf(all_digits, s) > 0:
        total_digits += 1
 
    # if character found in all_letters then increment total_letters by one
    elif op.countOf(all_letters, s) > 0:
        total_letters += 1
 
print("Total letters found :-", total_letters)
print("Total digits found :-", total_digits)


Output

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

Time Complexity: O(N)

Auxiliary Space : O(1)

Method 7:  using isalpha() and isdigit():

Python3




string = "Lazyroar2for3Lazyroar"
total_letters = sum([1 for char in string if char.isalpha()])
total_digits = sum([1 for char in string if char.isdigit()])
print("Total letters found :-", total_letters)
print("Total digits found :-", total_digits)
#This code is contributed by Jyothi pinjala


Output

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

Time Complexity: O(N)

Auxiliary Space : O(1)

Method 8:  Using try/except

Explanation:

In this approach, we use the try and except statements to determine if the character can be converted to an integer or not. If it can, it means the character is a digit, so we increment the total_digits count. If it can’t be converted, it means the character is a letter, so we increment the total_letters count.

Python3




# given string
string = "Lazyroar2for3Lazyroar"
  
# initialized value
total_digits = 0
total_letters = 0
  
# iterate through all characters
for s in string:
  
    # try to convert the character to int
    # if it's not possible, increment the letter count
    try:
        int(s)
        total_digits += 1
    except:
        total_letters += 1
  
print("Total letters found :-", total_letters)
print("Total digits found :-", total_digits)


Output

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

Time complexity: O(n)

Auxiliary Space: O(1)

RELATED ARTICLES

Most Popular

Recent Comments