# Python program to Count alphabets, digits, special char in string

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.

[elementor-template id=”5253″]

## 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

[elementor-template id=”5256″]

## Python code to count alphabets, digits, and special characters in the string

Output 1:

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 =3

[elementor-template id=”5257″]

Output 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