Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python regex to find sequences of one upper case letter followed by lower case letters
Next article icon

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

Last Updated : 06 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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

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]+$'. 

Python
import re  def findSequence(text):   	# regex pattern     pattern = '[A-Z][a-z]+'          # Use re.search to find any match     if re.search(pattern, text):         return 'Yes'     else:         return 'No'  # Driver program if __name__ == "__main__":     word = 'geeAkAA55of55gee4ksabc3Ar2x'     print(findSequence(word))     word = 'Geeks'     print(findSequence(word))     word = 'geeksforgeeks'     print(findSequence(word)) 

Output:

Yes
Yes
No

Time complexity: O(n), where n is length of string.
Auxiliary Space: O(1)



Find those sequences of one upper case letter followed by lower case letters

To find and print all sequences of one uppercase letter followed by lowercase letters in the given text we can use the re.findall() function.

Here's how we can do it:

Python
import re  def findSequences(text):   	#regex pattern     pattern = r'[A-Z][a-z]+'     return re.findall(pattern, text)  # Driver program if __name__ == "__main__":     word = 'geeAkAA55of55gee4ksabc3Ar2x'     print(findSequences(word))     word = 'Geeks'     print(findSequences(word))     word = 'geeksforgeeks'     print(findSequences(word)) 


Output:

['Ak', 'Ar']
['Geeks']
[]

Time Complexity: O(n) where n is the length of the input text.

Auxiliary Space Complexity: O(k) where k is the number of matches found.




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

S

Smitha Dinesh Semwal
Improve
Article Tags :
  • Python
  • Python Programs
  • python-regex
  • Python Regex-programs
Practice Tags :
  • python

Similar Reads

    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
    2 min read
    Verify that a string only contains letters, numbers, underscores and dashes - Python
    We are given a string and our task is to verify whether it contains only letters, numbers, underscores (_), and dashes (-). This kind of check is useful for validating usernames or formatted inputs. For example, a string like "G33ks_F0r-G3eks" should pass the validation, while a string like "Hello@1
    2 min read
    Python program to check if a string has at least one letter and one number
    The task is to verify whether a given string contains both at least one letter (either uppercase or lowercase) and at least one number. For example, if the input string is "Hello123", the program should return True since it contains both letters and numbers. On the other hand, a string like "Hello"
    3 min read
    Python program to check if lowercase letters exist in a string
    Checking for the presence of lowercase letters involves scanning the string and verifying if at least one character is a lowercase letter (from 'a' to 'z').Python provides simple and efficient ways to solve this problem using built-in string methods and constructs like loops and comprehensions.Using
    2 min read
    Python | Ways to check if given string contains only letter
    Given a string, write a Python program to find whether a string contains only letters and no other keywords. Let's discuss a few methods to complete the task. Method #1: Using isalpha() method Python3 # Python code to demonstrate # to find whether string contains # only letters # Initialising string
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences