In this article, we will discuss the concept of the Python code to Count Uppercase, Lowercase, Special character and Numeric values
In this post, we are going to learn how to count the total number of Uppercase, Lowercase, Special character and Numeric values of the given String in Python programming language
The program allows the user to enter a String and then it counts and display the total number of upper case, lower case, numeric values and special characters of the given string using for loop in Python programing language
Program 1
#Python code to Count Uppercase, Lowercase, Special character and Numeric values str=input("Please enter a string: ")#take input from the user upper, lower, num, special=0,0,0,0;#variable declaration and initilization for i in range(len(str)): if(str[i]>='A' and str[i]<='Z'):#check upper case letters upper+=1 elif(str[i]>='a' and str[i]<='z'):#check lower case letter lower+=1 elif(str[i]>='1' and str[i]<='9'):#check numeric value num+=1 else: special+=1 print("Upper case letters: ",upper) print("\nLower case letters: ",lower) print("\nnumbers: ",num) print("\nSpecial characters: ",special)
When the above code is executed, it produces the following result
Please enter a string: CodeForCoding:code4coding.com Upper case letters: 3 Lower case letters: 23 numbers: 1 Special characters: 2
Approach
The program allows the user to enter a String and then it counts and display the total number of upper case, lower case, numeric values and special characters of the given string using the built-in function in Python programing language
Program 2
#Python code to Count Uppercase, Lowercase, Special character and Numeric values str=input("Please enter a string: ") upper, lower, num, special=0,0,0,0; for i in range(len(str)): if(str[i].isupper()): upper+=1 elif(str[i].islower()): lower+=1 elif(str[i].isdigit()): num+=1 else: special+=1 print("Upper case letters: ",upper) print("\nLower case letters: ",lower) print("\nnumbers: ",num) print("\nSpecial characters: ",special)
When the above code is executed, it produces the following result
Please enter a string: MuCode:code4coding.com Upper case letters: 2 Lower case letters: 17 numbers: 1 Special characters: 2
Suggested for you
Similar post
C++ code to count the total number of characters in the given string
C code to count the total number of characters in the given string
Python code to count the total number of characters in the given string
Java program to count the total number of characters in the given string including space
C program to count the total number of characters in the given string including space
C++ program to count the total number of characters in the given string including space
10 best Ways to Subtract Two Numbers in Java (With Examples) In this article, we…
Array Data Structure: Definition, Types, Operations & Advantages Array Data Structure Introduction In this post,…
20 ways to subtract two numbers in Java In this article, we will discuss the…
10 simple ways to add two numbers in Java In this article, we will discuss…
Write a Python program to find the first n prime numbers In this article we…
Python: Calculate Average of odd and even in a list using loops In this post,…