# Count Alphabets & Digits from a string in Python using isalpha() & isdigit()

In this program, we will learn about Count Alphabets and Digits from a string in Python using isalpha() and isdigit(). So letâ€™s start and do program logic.

## Program to Count Alphabets and Digits from a string in Python using isalpha() and isdigit()

```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:Â Count Alphabets and Digits from a string in Python using isalpha() and isdigit()

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 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 if 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 the 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 as 3.