Count Alphabets and Digits from a string in Python

We want you to learn about the Count Alphabets and Digits from a string in Python. Hence we are come up with the best video explanation. Do you still face difficulties in understanding the logic? That’s perfectly fine; we have something more for you. You can scroll down to learn more about this topic.

For all the practice Videos and Explanations on Python, Check out Python Programming Exercises and Solutions.

Method 1: Count Alphabets and Digits from a string using isalpha() and isdigit() in Python

Source code:

st = input("Enter any sentence:")
a,d = 0, 0
for ch in st:
    if ch.isalpha():
        a+=1
    elif ch.isdigit():
        d+=1
print("Number of Alphabets:", a)
print("Number of Digits:",d)
Output:
Enter any sentence:Python 123
Number of Alphabets: 6
Number of Digits: 3

Check out our YouTube video here.

Code Explanation Method 1: Count Alphabets and Digits from a string using the Iterative method in Python

In this program, we are accepting a statement from the user and storing them into variable st, so we can count alphabets and digits from a string. In the next line, we have declared variables a and d to store the count of alphabets and digits in them and assign the value 0 to each of them. To count alphabets and digits, we have to iterate through strings. For this, we will use for loop.

We have for loop with ch as a counter variable, and this loop will iterate through the value of variable st. Inside for loop, we will check each letter to verify it is an alphabet or digit. For that, we have used the if condition and used the predefined function isalpha(). The isalpha() method returns True if all characters in the string are alphabets. If not, it returns False.

Return Value from isalpha()

The isalpha() returns:

  • True if all characters in the string are alphabets (can be both lowercase and uppercase).
  • False if at least one character is not an alphabet.

If ch is contained the alphabet, then the function will return true and we will increment the value of variable a, which is defined to store the count of the alphabet. In the next line, we have elif condition where we have checked if ch has digits; for this, we will use isdigit() function.

The isdigit() method returns True if all characters in a string are digits. If not, it returns False.

Return Value from isdigit()

The isdigit() returns:

  • True if all characters in the string are digits.
  • False if at least one character is not a digit.

If ch is contained Digits, then the function will return true and we will increment the value of variable d, which is defined to store the count of digits. In the next line, we will print the result of the total alphabet and digit from the string outside of the loop. Let’s run the program and enter the statement as Python 123. You will get the count of the alphabet as 6 and digits 3.

Method 2: Count Alphabets and Digits from a string in Python

Source code:

str = input("Enter the String: ")
alphabets = digits  = 0

for i in range(len(str)):
    if((str [i] >= 'a' and str [i] <= 'z') or (str [i] >= 'A' and str [i] <= 'Z')):
        alphabets = alphabets + 1
    elif(str [i] >= '0' and str [i] <= '9'):
        digits = digits + 1 

print("Total Number of Alphabets in this String :", alphabets)
print("Total Number of Digits in this String :", digits) 
Output
Enter the String: Python 123
Total Number of Alphabets in this String : 6
Total Number of Digits in this String : 3

Code Explanation Method 2: Count Alphabets and Digits from a string in Python

In this method, we have not used any pre-defined function over here. Here we have stored user input into str variable, and in the next line, we have defined variable alphabets and digit and assigned the value as 0. See here; if you check the last program, the way of defining variables is different than this one.

In the next line, we have used for loop to iterate through each letter of value containing str. This loop will run till the length of str. Here i is used as an index. Inside for loop, we have an if condition to check if the 0th index of str is between a to z or between A to Z. This means we are checking letter is between small a too small z or is it between capital A to capital Z.

If the above condition is true, then we will increment the value of the alphabet by 1. In the next line, we have elif condition to check if the next letter is f=digit or not. For that, we have checked if the letter is between 0 to. If the condition is true, then we will increment the digit by 1. So we will have the count of alphabets and digits. Outside of the for loop, we have print statements that print the result of the count.

Let’s run the program and enter the same string, Python 123. So, you will get output as.

Total Number of Alphabets in this String: 6
Total Number of Digits in this String : 3

I hope you got a clear understanding of calculating alphabets and digits in Python.

About The Author