Python regex to find sequences of one upper case letter followed by lower case letters

Write a Python Program to find sequences of one upper case letter followed by lower case letters. If found, print ‘Yes’, otherwise ‘No’.

Examples:

Input : Geeks
Output : Yes

Input : geeksforgeeks
Output : No

 

Approach: Using re.search()

To check if the sequence of one upper case letter followed by lower case letters we use regular expression ‘[A-Z]+[a-z]+$‘.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to find sequences of one upper
# case letter followed by lower case letters
import re
  
# Function to match the string
def match(text):
          
        # regex
        pattern = '[A-Z]+[a-z]+$'
          
        # searching pattern
        if re.search(pattern, text):
                return('Yes')
        else:
                return('No')
  
# Driver Function
print(match("Geeks"))
print(match("geeksforGeeks"))
print(match("geeks"))

chevron_right


Output:

Yes
Yes
No


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.