Signup/Sign In

How to Count Uppercase, Lowercase, special character and numeric values using Regex

In this tutorial, we will learn to count Uppercase, lowercase, special character, and numeric values using Regex.

The strings are wrapped under double or single quotes in python. The string may have upper case, lowercase, special character, and numeric values. We can find out the frequency of each type in the given string with python regex.

Python Regex also called REs or regular expressions is a module using which we can specify rules to set the possible strings to match. It is available in re module.

Look at the example to understand Input and output format.

Input: 'Study@123'

Output: Uppercase_count: 1

Lowercase_count: 4

Special_character_count: 1

number_count: 3

Input: 'ABCDabc123^&'

Output: Uppercase_count: 4

Lowercase_count: 3

Special_character_count: 2

number_count: 3

To execute this task, we will use find re.findall() method. It will find the substring which matches the regular expression and returns them as a list.

Approach: Using re.findall() method and len()

In this approach, we will find make the list of each character set using re.findall(pattern,string) method and then find the length of each character list using len() method.

Algorithm

Step1: Define a string with the character.

Step2: Create a separate list for each character set.

Step3: Use regex method to store the substring as a list.

Step4: Get the length of each variable set using len() and return them as output.

Python Program

In this example, we will take a string of characters and find a separate list for each type of character set. Further, we will count the frequency of each character set and print it as output.

import module for regular expression
import re
string= 'Study@123'
#list of each character set
upper= re.findall(r"[A-Z]", string)
lower = re.findall(r"[a-z]", string)
special = re.findall(r"[,@$%#*.!?]", string)
number = re.findall(r"[0-9]", string)

# Count for each character set
print("Uppercase_count:", len(upper))
print("lowercase_count:", len(lower))
print("special_character_count:", len(special))
print("number_count:", len(number))

Output

Here is the output of the above code.

count character set

Python program 2

Here is another set of examples with different string values.

# import module for regular expression
import re
string= 'ABCBabc123^&'
#list of each character set
upper= re.findall(r"[A-Z]", string)
lower = re.findall(r"[a-z]", string)
special = re.findall(r"[,@$%#*^&.!?]", string)
number = re.findall(r"[0-9]", string)

# Count for each character set
print("Uppercase_count:", len(upper))
print("lowercase_count:", len(lower))
print("special_character_count:", len(special))
print("number_count:", len(number))

Output

Here is the output of the above code.

Character count

Conclusion

In this tutorial, we have learned to count Uppercase, lowercase, special character, and number in the given string. We can do so using regex.