Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Match Any One Uppercase Character in Python Using Regular Expression



There are two ways to match any one uppercase character in python using Regular Expression. One is the general method and other is using the regular expression.

A regular expression is a series of characters that allows you to discover a string or a set of strings using a search pattern. Regular expressions are sometimes known as RegEx. In Python, the re module is used to work with regular expressions.

How to Match an Uppercase Character?

In this article we will match the uppercase vowel in Python using a regular expression. To do this we will use r'[A,E,I,O,U]' regular expression. Here, we have used a set of uppercase vowels [A,E,I,O,U] as we need to match them. In the following code, lets us see how to match lowercase vowels.

Python provides a built-in module for regex -

import re

Example: Match Uppercase in a Sentence

In the following code, let us see how to match uppercase vowels using regular expression. We begin by importing regular expression module.

Then, we have used findall() function which is imported from the re module.

import re
string = 'TutRoIals POinT Is A GreAt PlaTfOrm to lEarN'
res = re.findall(r'[A,E,I,O,U]', string)
print (res)

On executing the above program, the list of uppercase vowels is printed as output -

['I', 'O', 'I', 'A', 'A', 'O', 'E']

Example: Match an Uppercase Letter at the Start of a Word

In the following code, we will check if the first character of a string is uppercase. Here we are using the re.match() function to check if the first character matches the pattern.

import re
string = 'Python is a programming language'
if re.match(r'^[A-Z]', string):
    print("The first character is uppercase.")
else:
    print("The first character is not uppercase.")

On executing the above program, the output will be -

The first character is not uppercase.

Example: Count Total Uppercase Letters

In the following code we will count the total number of uppercase letters in a string. Here we are using the re.findall() function to find all uppercase letters and then count them using the len() function.

import re
string = 'TutRoIals POinT Is A GreAt PlaTfOrm to lEarN'
uppercase_letters = re.findall(r'[A-Z]', string)
count = len(uppercase_letters)
print("Total uppercase letters:", count)

After running the program, the output will be as follows -

Total uppercase letters: 8

Using General method

In the following code, we have matched all the uppercase vowels from the string 'TutRoIals POinT Is A GreAt PlaTfOrm to lEarN'. Here we did not use regular expression to match.

Example

The following example is a program which shows the matching of any one uppercase character in python using the general method.

string='TutRoIals POinT Is A GreAt PlaTfOrm to lEarN'
lst = []
for i in string:
   if (i=='A' or i=='E' or i=='I' or i=='O' or i=='U'):
      lst.append(i)
print(lst)

On running the above program, the list of uppercase vowels is printed as output -

['I', 'O', 'I', 'A', 'A', 'O', 'E']
Updated on: 2025-04-23T14:49:17+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements