;

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


Tutorialsrack 27/04/2020 Python

In this Python program, we will learn how to count the total number of alphabets, digits, and special characters in a given string. In this program, we will use loops and built-in python functions to count the total number of alphabets, digits, and special characters in a given string.

Here is the Source code of the program to count the total number of alphabets, digits, and special characters in a given string.

Program 1: To Count Alphabets, Digits and Special Characters in a String using For Loop, isalpha() and isdigit() function

To Count Alphabets, Digits and Special Characters in a String using For Loop, isalpha() and isdigit() function

# To Count Alphabets, Digits and Special Characters in a String using For Loop,
# isalpha() and isdigit() function

# Take the Input From the User
string = input("Enter the 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)
Output

Enter the String: Tutorials Rack is an Online Learning Website

 

Total Number of Alphabets in this String:   38

Total Number of Digits in this String:   0

Total Number of Special Characters in this String:   6

Program 2: Python Program to Count an Alphabets, Digits and Special Characters in a Given String

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

# Python Program to Count an Alphabets, Digits and Special Characters in a Given String
# using loop with range function

# Take the Input From the User
str1 = input("Enter the 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)
Output

Enter the String: Tutorials Rack is an Online Learning Website

 

Total Number of Alphabets in this String :   38

Total Number of Digits in this String :   0

Total Number of Special Characters in this String :   6


Related Posts



Comments

Recent Posts
Tags