Python Program to Count Uppercase, Lowercase, Special Character and Numeric Values Using Regex
Given a string, task is to count the number of uppercase letters, lowercase letters, numeric digits, and special characters in a given string using Regular Expressions (Regex). For Examples:
Input: "ThisIsGeeksforGeeks!, 123"
Output: No. of uppercase characters = 4
No. of lowercase characters = 15
No. of numerical characters = 3
No. of special characters = 2
Let's explore different methods to do this task efficiently in Python.
Using re.findall()
The re.findall() function finds all non-overlapping matches of a pattern in a string and returns them as a list. We can use different regex patterns to extract uppercase, lowercase, numeric, and special characters separately and then count their occurrences using len().
import re
s = "ThisIsGeeksforGeeks !, 123"
a = re.findall(r"[A-Z]", s)
b = re.findall(r"[a-z]", s)
c = re.findall(r"[0-9]", s)
d = re.findall(r"[, .!?]", s) # includes space as a special character
print("No. of uppercase characters:", len(a))
print("No. of lowercase characters:", len(b))
print("No. of numerical characters:", len(c))
print("No. of special characters:", len(d))
Output
No. of uppercase characters: 4 No. of lowercase characters: 15 No. of numerical characters: 3 No. of special characters: 4
Using re.finditer()
The re.finditer() function returns an iterator yielding match objects. It allows more control over each match and can be useful when processing large strings efficiently.
import re
s = "ThisIsGeeksforGeeks !, 123"
a = len(list(re.finditer(r"[A-Z]", s)))
b = len(list(re.finditer(r"[a-z]", s)))
c = len(list(re.finditer(r"[0-9]", s)))
d = len(list(re.finditer(r"[, .!?]", s))) # includes space as a special character
print("No. of uppercase characters:", a)
print("No. of lowercase characters:", b)
print("No. of numerical characters:", c)
print("No. of special characters:", d)
Output
No. of uppercase characters: 4 No. of lowercase characters: 15 No. of numerical characters: 3 No. of special characters: 4
Using re.sub() and String Length Difference
This method uses re.sub() to remove unwanted characters and counts the remaining ones by comparing string lengths. It is slightly less efficient but still effective when regex filtering is preferred.
import re
s = "ThisIsGeeksforGeeks !, 123"
a = len(re.sub(r"[^A-Z]", "", s))
b = len(re.sub(r"[^a-z]", "", s))
c = len(re.sub(r"[^0-9]", "", s))
d = len(re.sub(r"[A-Za-z0-9]", "", s))
print("No. of uppercase characters:", a)
print("No. of lowercase characters:", b)
print("No. of numerical characters:", c)
print("No. of special characters:", d)
Output
No. of uppercase characters: 4 No. of lowercase characters: 15 No. of numerical characters: 3 No. of special characters: 4