Open In App

Python Regex to Find Sequences of One Uppercase Letter Followed by Lower Case Letters

Last Updated : 08 Nov, 2025
Comments
Improve
Suggest changes
3 Likes
Like
Report

Given a string containing a mix of uppercase and lowercase letters, the task is to extract all sequences where one uppercase letter is immediately followed by one or more lowercase letters using Regex in Python. For example:

Input: geeAkAA55of55gee4ksabc3Ar2x
Output: ['Ak', 'Ar']

Let’s explore different methods to check and extract such sequences efficiently in Python.

Using re.finditer()

The re.finditer() method finds all matches like re.findall(), but instead of returning a list, it yields match objects, which can be processed one by one.

Python
import re
s = 'geeAkAA55of55gee4ksabc3Ar2x'
for match in re.finditer(r'[A-Z][a-z]+', s):
    print(match.group())

Output
Ak
Ar

Explanation:

  • re.finditer(r'[A-Z][a-z]+', s) returns an iterator with all matches.
  • Each match.group() retrieves the actual matched substring.

Using re.findall()

The re.findall() method directly extracts all sequences that match a given pattern and returns them as a list.

Python
import re
s = 'geeAkAA55of55gee4ksabc3Ar2x'
res = re.findall(r'[A-Z][a-z]+', s)
print(res)

Output
['Ak', 'Ar']

Explanation:

  • r'[A-Z][a-z]+' matches one uppercase letter followed by one or more lowercase letters.
  • re.findall() scans the entire string and returns a list of all such substrings.

Using re.findall() with Word Boundaries

This variation ensures that each valid sequence is treated as a standalone word by using word boundary anchors (\b). It’s especially useful when such patterns appear next to numbers or symbols.

Python
import re
s = 'gee Ak AA55 of 55 gee4 ks abc3 Ar 2x'
res = re.findall(r'\b[A-Z][a-z]+\b', s)
print(res)

Output
['Ak', 'Ar']

Explanation:

  • \b ensures that the match starts and ends at word boundaries.
  • This ensures that only standalone capitalized words are matched, excluding patterns embedded within other words or digits.

Explore