Open In App

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

Last Updated : 28 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Regular Expression in Python

Given a string. The task is to count the number of Uppercase, Lowercase, special character and numeric values present in the string using Regular expression in Python.

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

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

Example 1:

The re.findall(pattern, string, flags=0) method can be used to find all non-overlapping matches of a pattern in the string. The return value is a list of strings.
Below is the implementation. 
 

Python3




import re
 
 
string = "ThisIsGeeksforGeeks !, 123"
 
# Creating separate lists using
# the re.findall() method.
uppercase_characters = re.findall(r"[A-Z]", string)
lowercase_characters = re.findall(r"[a-z]", string)
numerical_characters = re.findall(r"[0-9]", string)
special_characters = re.findall(r"[, .!?]", string)
 
print("The no. of uppercase characters is", len(uppercase_characters))
print("The no. of lowercase characters is", len(lowercase_characters))
print("The no. of numerical characters is", len(numerical_characters))
print("The no. of special characters is", len(special_characters))


Output:

The no. of uppercase characters is 4
The no. of lowercase characters is 15
The no. of numerical characters is 3
The no. of special characters is 4

Time complexity :  O(n)

Space Complexity : O(n)

Example 2:

Python3




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_char_count = len(re.findall(r'[!@#$%^&*()_+{}\[\]:;<>,.?\\/-]', input_string))
    numeric_count = len(re.findall(r'[0-9]', input_string))
     
    return uppercase_count, lowercase_count, special_char_count, numeric_count
 
test_string = "Hello World! 123"
uppercase, lowercase, special_chars, numeric = count_characters(test_string)
 
print("Uppercase count:", uppercase)
print("Lowercase count:", lowercase)
print("Special character count:", special_chars)
print("Numeric count:", numeric)


Output:

Uppercase count: 2
Lowercase count: 8
Special character count: 1
Numeric count: 3



Previous Article
Next Article

Similar Reads

Python Regex to extract maximum numeric value from a string
Given an alphanumeric string, extract maximum numeric value from that string. Alphabets will only be in lower case. Examples: Input : 100klh564abc365bgOutput : 564Maximum numeric value among 100, 564 and 365 is 564.Input : abchsd0sdhsOutput : 0Python Regex to extract maximum numeric value from a stringThis problem has existing solution please refer
2 min read
Python Regex | Program to accept string ending with alphanumeric character
Prerequisite: Regular expression in PythonGiven a string, write a Python program to check whether the given string is ending with only alphanumeric character or Not.Examples: Input: ankitrai326 Output: Accept Input: ankirai@ Output: Discard In this program, we are using search() method of re module. re.search() : This method either returns None (if
2 min read
Convert Factor to Numeric and Numeric to Factor in R Programming
Factors are data structures that are implemented to categorize the data or represent categorical data and store it on multiple levels. They can be stored as integers with a corresponding label to every unique integer. Though factors may look similar to character vectors, they are integers, and care must be taken while using them as strings. The fac
5 min read
Python | Lowercase first character of String
The problem of capitalizing a string is quite common and has been discussed many times. But sometimes, we might have a problem like this in which we need to convert the first character of the string to lowercase. Let us discuss certain ways in which this can be performed. Method #1: Using string slicing + lower() This task can easily be performed u
4 min read
Python - Lowercase Kth Character in string
The problem of lowercasing a string is quite common and has been discussed many times. But sometimes, we might have a problem like this in which we need to convert the Nth character of string to lowercase. Let’s discuss certain ways in which this can be performed. Method #1 : Using string slicing + lower() This task can easily be performed using th
4 min read
Python - Test if String contains any Uppercase character
Given a String, Test if it contains any uppercase character. Input : test_str = 'geeksforgeeks' Output : False Explanation : No uppercase character in String.Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String. Method #1 : Using loop + isupper() In this, we iterate for each character in String, check for uppercas
6 min read
Python - Uppercase Nth character
The problem of capitalizing a string is quite common and has been discussed many times. But sometimes, we might have a problem like this in which we need to convert the Nth character of the string to uppercase. Let’s discuss certain ways in which this can be performed. Method #1 : Using string slicing + upper() This task can easily be performed usi
5 min read
Replace values in Pandas dataframe using regex
While working with large sets of data, it often contains text data and in many cases, those texts are not pretty at all. The text is often in very messier form and we need to clean those data before we can do anything meaningful with that text data. Mostly the text corpus is so large that we cannot manually list out all the texts that we want to re
4 min read
Program to check if a string contains any special character
Given a string, the task is to check if that string contains any special character (defined special character set). If any special character is found, don't accept that string. Examples : Input: Geeks$For$GeeksOutput: String is not accepted. Input: Geeks For GeeksOutput: String is accepted Approach 1: Using regular expression Make a regular express
14 min read
Python | Program that matches a word containing 'g' followed by one or more e's using regex
Prerequisites : Regular Expressions | Set 1, Set 2 Given a string, the task is to check if that string contains any g followed by one or more e's in it, otherwise, print No match. Examples : Input : geeks for geeks Output : geeks geeks Input : graphic era Output : No match Approach : Firstly, make a regular expression (regex) object that matches a
2 min read
three90RightbarBannerImg