Python Program to Count Alphabets Digits and Special Characters in a String

Write a Python program to Count Alphabets Digits and Special Characters in a String using For Loop, while loop, and Functions with an example.

Python Program to Count Alphabets Digits and Special Characters in a String using For Loop

This python program allows the user to enter a string.

First, we used For Loop to iterate characters in a String. Inside the For Loop, we are using Elif Statement

  • isalpha() in the first statement is to check whether the character is an alphabet or not. If true, alphabets value incremented.
  • isdigit() in the second statement checks whether the character is Digit or not. If true, digits value incremented.
  • Otherwise, special characters value incremented.
# Python program to Count Alphabets Digits and Special Characters in a String
 
string = input("Please Enter your Own String : ")
alphabets = digits = special = 0

for i in range(len(string)):
    if(string[i].isalpha()):
        alphabets = alphabets + 1
    elif(string[i].isdigit()):
        digits = digits + 1
    else:
        special = special + 1
        
print("\nTotal Number of Alphabets in this String :  ", alphabets)
print("Total Number of Digits in this String :  ", digits)
print("Total Number of Special Characters in this String :  ", special)

Python Count Alphabets, Digits, and Special Characters in a String output

Please Enter your Own String : abc!@ 12 cd 1212

Total Number of Alphabets in this String :   5
Total Number of Digits in this String :   6
Total Number of Special Characters in this String :   5

Python Program to Count Alphabets Digits and Special Characters using While Loop

In this Python count alphabets, digits, and special characters program, we are comparing each character with a, A, z, Z, 0, and 9. Based on the result, we are incrementing the corresponding values.

# Python Program to Count Alphabets Digits and Special Characters in a String

 str1 = input("Please Enter your Own String : ")
alphabets = digits = special = 0

for i in range(len(str1)):
    if((str1[i] >= 'a' and str1[i] <= 'z') or (str1[i] >= 'A' and str1[i] <= 'Z')): 
        alphabets = alphabets + 1 
    elif(str1[i] >= '0' and str1[i] <= '9'):
        digits = digits + 1
    else:
        special = special + 1
        
print("\nTotal Number of Alphabets in this String :  ", alphabets)
print("Total Number of Digits in this String :  ", digits)
print("Total Number of Special Characters in this String :  ", special)
Python Program to Count Alphabets Digits and Special Characters in a String 2

Program to Count Alphabets Digits and Special Characters in a String using Function

In this program, we are comparing each character with ASCII values to find the alphabet, digit, and special characters in this string.

# Python Program to Count Alphabets Digits and Special Characters in a String
 
str1 = input("Please Enter your Own String : ")
alphabets = digits = special = 0

for i in range(len(str1)):
    if(ord(str1[i]) >= 48 and ord(str1[i]) <= 57): 
       digits = digits + 1 
    elif((ord(str1[i]) >= 65 and ord(str1[i]) <= 90) or (ord(str1[i]) >= 97 and ord(str1[i]) <= 122)):
        alphabets = alphabets + 1
    else:
        special = special + 1
        
print("\nTotal Number of Alphabets in this String :  ", alphabets)
print("Total Number of Digits in this String :  ", digits)
print("Total Number of Special Characters in this String :  ", special)

Python Count Alphabets, Digits, and Special Characters in a String output

Please Enter your Own String : abcd*()_+12211!!!@sid4

Total Number of Alphabets in this String :   7
Total Number of Digits in this String :   6
Total Number of Special Characters in this String :   9