Python Program to Calculate the Number of Digits and Letters in a String

We want you to know about the Count Alphabets and Digits from a string in Python, hence we are come up with the best video explanation you have ever watched on the internet, where you not only understand the logic but also code to get the desired output.

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 Explanation on Python please click over here. Python Practice Series.

Video Explanation to Count Alphabets and Digits from a string in Python

Code: Count Alphabets and Digits from a string in Python

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

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

In this program, we are accepting a statement from the user and store 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 into them and assigned value 0 to each of them.

To count alphabets and digits we have to iterate through string for this we will use for loop.

We have for loop with ch as counter variable and this loop will iterate through value of variable st.

Inside for loop we will check each letter for verifying is it a alphabet or digit. For that we have used if condition and used 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 contains alphabet, then function will return true and we will increment value of variable a, which is defined to store count of alphabet.

In the next line we have elif condition where we have checked if ch have 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 contains Digits, then function will return true and we will increment value of variable d, which is defined to store count of digit.

In the next line we will print the result of total alphabet and digit from string outside of the for loop.

Let’s run the program and enter statement as Python 123.

You will get count of 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 value as 0.

Look here, if you check last program, way of defining variable 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 use as index.

Inside for loop we have a if condition to check if 0th index of str is between a to z or between A to Z. Which means we are checking letter is between small a to small z or is it between capital A to capital Z.

If above condition is true then we will increment value of 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 digit.

Outside of the for loop we have print statements which are printing result of the count.

Let’s run program and enter 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 the clear understanding of calculating alphabets and digits in Python.

Facebook Comments
(Visited 1 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *