In this tutorial, we are going to learn python program to count the number of alphabets, digits, and special characters present in an input string from the user.
Problem Statement
For any input string, we are going to print the number of alphabets (‘a’-‘z’ and ‘A’-‘Z’), the number of digits(0-9), and the number of special characters present.
For example:
Case1: If the user inputs the string ‘%python123%$#’,
the output should be, alphabets=5, digits=3, special characters=4.
Case2: If the user inputs the string ‘!!quescollll151!!%&’,
the output should be, alphabets=10, digits=3, special characters=6.
Our logic to count alphabets, digits, and special characters in the string
- Our program will take a string as an input from the user.
- Count the number of alphabets, digits, and special symbols and store it in different variables. This can be done using string iteration using the ‘for’ loop.
- The built-in functions ‘isalpha()’ and ‘isdigit()’ help to identify the alphabets and digits used in string characters.
Algorithm to count alphabets, digits, and special characters in the string
Step1: Start
Step2: Take a string as an input from the user.
Step3: Create 3 variables to count the numbers of alphabets, digits, and special characters and assign the value ‘0’ to them.
Step4: Use the ‘for’ loop to iterate through the string.
Step5: if i.isalpha():
alpha+=1
elif i.isdigit():
digit+=1
else:
specialChar +=1
Step6: Print ‘alpha’, ‘digit’, and ‘special_char’
Step7: Stop
Python Program to Count Alphabets, Digits, and Special characters in the String
Program 1: Using isalpha() and isdigit() method
string = input("Enter a String : ")
alphabets=0
digits=0
specialChars=0
#checks for each character in the string
for i in string:
#if character of the string is an alphabet
if i.isalpha():
alphabets+=1
#if character of the string is a digit
elif i.isdigit():
digits+=1
else: #if character of the string is a special character
specialChars+=1
print("alphabets =",alphabets,"digits =",digits,"specialChars =",specialChars)
Output 1:
Enter a String : Ques123!@we12 alphabets = 6 digits = 5 specialChars = 2
Explanation:
For the input string ‘Ques123!@we12’, the alphabets used in this string are ‘Q’, ‘u’, ‘e’, ‘s’, ‘w’, ‘e’. The digits used in this string are ‘1’, ‘2’, ‘3’, ‘1’, and ‘2’.
And the special character used in this string is ‘!’, and ‘@’.
That makes the number of alphabet = 6,
the number of digits = 5
the number of special characters = 2
Output 2:
Enter a String : python is fun alphabets = 11 digits = 0 specialChars = 2
Explanation:
For the input string ‘python is fun’, the alphabets used in this string are ‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’, ‘i’, ‘s’, ‘f’, ‘u’, and ‘n’. There is no digit used in this string
And the special character used in this string is ‘ ’(spaces).
That makes the number of alphabet =11,
the number of digits =0,
the number of special characters =2
Program 2: Using Regular Expressions
This approach utilizes the re
module to find matches for alphabets, digits, and special characters using regular expressions.
import re
string = input("Enter a String : ")
alphabets = len(re.findall(r'[a-zA-Z]', string))
digits = len(re.findall(r'\d', string))
specialChars = len(re.findall(r'\W', string))
print("alphabets =",alphabets,"digits =",digits,"specialChars =",specialChars)
Output
Enter a String : Ques@#123 alphabets = 4 digits = 3 specialChars = 2
Program 3: Using Collections.Counter
This approach provides a detailed count of each type of character using the Counter
from the collections
module.
from collections import Counter
input_string = input("Enter a String : ")
count = Counter(input_string)
alphabets = sum(count[char] for char in count if char.isalpha())
digits = sum(count[char] for char in count if char.isdigit())
specialChars = sum(count[char] for char in count if not char.isalnum() and not char.isspace())
print("alphabets =",alphabets,"digits =",digits,"specialChars =",specialChars)
Output
Enter a String : quesc123@# alphabets = 5 digits = 3 specialChars = 2