Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Looping Statements

Count digits, alphabets, and special characters in a string - Python Program

Example 1 :

# Count digits, alphabets, and special characters text = input("Enter a string: ") digits = alphabets = specials = 0 for char in text: if char.isdigit(): digits += 1 elif char.isalpha(): alphabets += 1 else: specials += 1 print(f"Alphabets: {alphabets}") print(f"Digits: {digits}") print(f"Special Characters: {specials}")

Output

 
OUTPUT  :
Enter a string: Hello123@Python!
Alphabets: 11
Digits: 3
Special Characters: 2
 

Explanation

  • .isdigit() → checks for numeric characters.
  • .isalpha() → checks for alphabets (A–Z, a–z).
  • Anything else is counted as special characters.
  • O(n) complexity where n = length of string.