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. The pattern is used to match and manipulate text strings, which can be very useful 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 (regex), we can use specific patterns to match and count the desired characters. 

Following are the patterns an explanation to count uppercase letters, lowercase letters, special characters, and numeric values using regular expressions:

  • Uppercase Letters −

    • Pattern − [A-Z]

    • Explanation − The pattern [A-Z] matches any uppercase letter from A to Z. The hyphen - specifies a range of characters. So, this pattern matches any uppercase letter in the input string.

  • Lowercase Letters −

    • Pattern − [a-z]

    • Explanation − The pattern [a-z] matches any lowercase letter from a to z. Similar to uppercase letters, this pattern captures any lowercase letter in the input string.

  • Special Characters −

    • Pattern − [^A-Za-z0-9]

    • Explanation − The pattern [^A-Za-z0-9] matches any character that is not an uppercase letter, lowercase letter, or numeric digit. The caret ^ inside the square brackets [^ ] denotes negation. Therefore, this pattern matches any special character that is not in the range of A-Z, a-z, or 0-9.

  • Numeric Values −

    • Pattern − [0-9]

    • Explanation − The pattern matches any numeric digit from 0 to 9. 

To count the occurrences of each category, we can use the re.findall() function from the re module in Python. This function searches the input string for all non-overlapping occurrences of a pattern and returns them as a list. The length of the resulting list gives us the count of occurrences.

Input Output scenarios

Let's explore some input-output scenarios to count the number of uppercase letters, lowercase letters, special characters, and numeric values in a given string.

Scenario 1 −

Input string: Hello World!
Output:
Uppercase letters: 2
Lowercase letters: 8
Special characters: 1
Numeric values: 0

The input 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 input 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).

Example

Let’s take an example to count the number of uppercase letters, lowercase letters, special characters, and numeric values in a given 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)

Output

Uppercase letters: 1
Lowercase letters: 11
Special characters: 1
Numeric values: 2

Example

In this example, we will count the number of uppercase letters, lowercase letters, special characters, and numeric values in a text file. Following is the text data present in the text file:

import re

def count_characters(filename):
    with open(filename, 'r') as file:
        for line in file:
            uppercase_count = len(re.findall(r'[A-Z]', line))
            lowercase_count = len(re.findall(r'[a-z]', line))
            special_count = len(re.findall(r'[^A-Za-z0-9]', line))
            numeric_count = len(re.findall(r'[0-9]', line))

    return uppercase_count, lowercase_count, special_count, numeric_count

# Provide the path of the text file
file = 'Example_text_file.txt'

# Call the function to count Uppercase, Lowercase, special character and numeric values 
upper, lower, special, numeric = count_characters(file)

print("Uppercase letters:", upper)
print("Lowercase letters:", lower)
print("Special characters:", special)
print("Numeric values:", numeric)

Output

Uppercase letters: 1
Lowercase letters: 4
Special characters: 4
Numeric values: 4

Updated on: 29-Aug-2023

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements