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.

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

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 methodin 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 into them and assigned 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 counter variable and this loop will iterate through the value of variable st.

Inside for loop, we will check each letter for verifying is it an 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 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 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 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 for loop.

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

You will get the 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 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 use 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 are printing 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.