Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to Count Uppercase, Lowercase, special character and numeric values using Regex
Regular expressions, commonly known as re or Regex is a powerful tool for manipulating and searching for patterns in text. In Python, regular expressions are implemented using the re module. A regular expression is a sequence of characters that define a search pattern used to match and manipulate text strings for tasks such as data cleaning, parsing, and validation.
To count the number of uppercase letters, lowercase letters, special characters, and numeric values in a string using regular expressions, we use specific patterns to match and count the desired characters.
Regex Patterns for Character Counting
-
Uppercase Letters
Pattern:
[A-Z]Matches any uppercase letter from A to Z. The hyphen specifies a range of characters.
-
Lowercase Letters
Pattern:
[a-z]Matches any lowercase letter from a to z.
-
Special Characters
Pattern:
[^A-Za-z0-9]Matches any character that is not an uppercase letter, lowercase letter, or numeric digit. The caret
^inside square brackets denotes negation.
-
Numeric Values
Pattern:
[0-9]Matches any numeric digit from 0 to 9.
We use the re.findall() function to search the input string for all non-overlapping occurrences of a pattern. The length of the resulting list gives us the count of occurrences.
Input Output Scenarios
Scenario 1:
Input string: "Hello World!" Output: Uppercase letters: 2 Lowercase letters: 8 Special characters: 1 Numeric values: 0
The string "Hello World!" has 2 uppercase letters (H and W), 8 lowercase letters (e, l, l, o, o, r, l, d), 1 special character (!), and 0 numeric values.
Scenario 2:
Input string: "@#Hello1234#@" Output: Uppercase letters: 1 Lowercase letters: 4 Special characters: 4 Numeric values: 4
The string "@#Hello1234#@" has 1 uppercase letter (H), 4 lowercase letters (e, l, l, o), 4 special characters (@, #, #, @), and 4 numeric values (1, 2, 3, 4).
Basic Character Counting Example
Here's how to count different character types in a string ?
import re
def count_characters(input_string):
uppercase_count = len(re.findall(r'[A-Z]', input_string))
lowercase_count = len(re.findall(r'[a-z]', input_string))
special_count = len(re.findall(r'[^A-Za-z0-9]', input_string))
numeric_count = len(re.findall(r'[0-9]', input_string))
return uppercase_count, lowercase_count, special_count, numeric_count
# Define the input string
input_str = 'Tutor1als!p0int'
upper, lower, special, numeric = count_characters(input_str)
print("Uppercase letters:", upper)
print("Lowercase letters:", lower)
print("Special characters:", special)
print("Numeric values:", numeric)
Uppercase letters: 1 Lowercase letters: 11 Special characters: 1 Numeric values: 2
Processing Multiple Strings
Here's how to analyze multiple strings and display detailed results ?
import re
def analyze_string(text):
uppercase = len(re.findall(r'[A-Z]', text))
lowercase = len(re.findall(r'[a-z]', text))
special = len(re.findall(r'[^A-Za-z0-9]', text))
numeric = len(re.findall(r'[0-9]', text))
return {
'uppercase': uppercase,
'lowercase': lowercase,
'special': special,
'numeric': numeric
}
# Test with multiple strings
test_strings = [
"Hello123!",
"PYTHON@2023",
"data_science$",
"ABC123xyz@#$"
]
for i, text in enumerate(test_strings, 1):
result = analyze_string(text)
print(f"String {i}: '{text}'")
print(f" Uppercase: {result['uppercase']}")
print(f" Lowercase: {result['lowercase']}")
print(f" Special: {result['special']}")
print(f" Numeric: {result['numeric']}")
print()
String 1: 'Hello123!' Uppercase: 1 Lowercase: 4 Special: 1 Numeric: 3 String 2: 'PYTHON@2023' Uppercase: 6 Lowercase: 0 Special: 1 Numeric: 4 String 3: 'data_science$' Uppercase: 0 Lowercase: 11 Special: 2 Numeric: 0 String 4: 'ABC123xyz@#$' Uppercase: 3 Lowercase: 3 Special: 3 Numeric: 3
Alternative Approach Using Single Regex
We can optimize the counting process by using a single pass through the string ?
import re
def count_chars_optimized(text):
uppercase = lowercase = special = numeric = 0
for char in text:
if re.match(r'[A-Z]', char):
uppercase += 1
elif re.match(r'[a-z]', char):
lowercase += 1
elif re.match(r'[0-9]', char):
numeric += 1
else:
special += 1
return uppercase, lowercase, special, numeric
# Test the optimized function
text = "Hello World! 123"
upper, lower, special, numeric = count_chars_optimized(text)
print(f"Text: '{text}'")
print(f"Uppercase: {upper}")
print(f"Lowercase: {lower}")
print(f"Special: {special}")
print(f"Numeric: {numeric}")
Text: 'Hello World! 123' Uppercase: 2 Lowercase: 8 Special: 2 Numeric: 3
Conclusion
Regular expressions provide an efficient way to count different character types in strings using patterns like [A-Z], [a-z], [0-9], and [^A-Za-z0-9]. Use re.findall() with len() for simple counting or iterate through characters for optimized performance with large texts.
