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.
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
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…