String

Python code to Count Uppercase, Lowercase,Special character and Numeric values

Python code to Count Uppercase, Lowercase, Special character and Numeric values

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

Count upper, lower,numeric and special character

Python code to Count Uppercase, Lowercase, Special character and Numeric values using for loop

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

  1. Declare a String variable as str;
  2. Declare and initialize integer variables as int upper, lower, num, Special=0,0,0,0;
  3. The user asked to enter a string to count upper case, lower case, Numeric, Special characters
  4. The given string is stored in the variable str;
  5. A for-loop is used to count total characters of the given string.
  6. Use an if condition to test if(str[i]>=’A’ and str[i]<=’Z’). If it is true,  The upper becomes upper + 1(upper=upper+1);
  7. When it is false, control moves to the elif part and evaluates the test-expression of elif. if it is true The lower becomes lower + 1(lower=lower+1
  8. When it is false, control moves to the next elif part and evaluates the test-expression of elif. if it is true The num becomes num+1(num=num+1);
  9. When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
  10. Finally, the program displays the total number of the upper case, lower case, Numeric, Special characters of the given string

Python code to Count Uppercase, Lowercase, Special character and Numeric values using the built-in function

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

for loop in Python language

while loop in Python language

 

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

 

Count Uppercase, Lowercase,Special character and Numeric values in Java
Program to Count Uppercase, Lowercase,Special character and Numeric values in C
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago