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:
re.findall() in Python
Next article icon

re.findall() in Python

Last Updated : 30 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

re.findall() method in Python helps us find all pattern occurrences in a string. It's like searching through a sentence to find every word that matches a specific rule. We can do this using regular expressions (regex) to create the pattern and then use re.findall() to get a list of matches.

Let's say we want to find all words that start with "P" in a string.

Python
import re  s = "Python is Powerful and Great" result = re.findall(r'\bP\w+', s) print(result) 

Output
['Python', 'Powerful'] 

Table of Content

  • Syntax of re.findall()
  • Case Sensitivity in re.findall()
  • Finding numbers using re.findall()
  • Matching dates using re.findall()

Syntax of re.findall()

The syntax of re.findall() is simple:

import re

result = re.findall(pattern, string)

Parameters

  • pattern: The regular expression pattern we are looking for.
  • string: The string where we want to search for the pattern.
  • result: This will be a list of all occurrences of the pattern in the string.

Return

The return type of re.findall() is always a list. This list contains all the non-overlapping matches of the pattern in the string. If no match is found, it will return an empty list.

Case Sensitivity in re.findall()

By default, re.findall() is case-sensitive. To make it case-insensitive, we can pass the re.IGNORECASE flag.

Python
import re  s = "Python is fun. python is cool." result = re.findall(r'python', s, re.IGNORECASE) print(result) 

Output
['Python', 'python'] 

Finding numbers using re.findall()

Here we use a pattern that matches any number. The regular expression \d+ looks for one or more digits in a row. So if the text contains numbers like "12 apples and 5 bananas", it will find "12" and "5" and give us those as the result

Python
import re  # Find all numbers in a string text = "There are 12 apples, 5 bananas, and 300 oranges."  # \d+ matches one or more digits pattern = r'\d+'   matches = re.findall(pattern, text) print(matches)   

Output
['12', '5', '300'] 

Matching dates using re.findall()

Here we use a pattern to find dates written in the format YYYY-MM-DD. The regular expression \d{4}-\d{2}-\d{2} is designed to find a four-digit year, followed by a two-digit month and a two-digit day. If the text contains "2024-12-25" and "2025-01-15" it will pick them out as matches.

Python
import re  #Find all dates in YYYY-MM-DD format text = "The event is on 2024-12-25, and the deadline is 2025-01-15."  # Matches a date in YYYY-MM-DD format pattern = r'\d{4}-\d{2}-\d{2}'   matches = re.findall(pattern, text) print(matches)   

Output
['2024-12-25', '2025-01-15'] 

Next Article
re.findall() in Python

P

pragya22r4
Improve
Article Tags :
  • Python
  • python-regex
  • python
Practice Tags :
  • python
  • python

Similar Reads

    re.finditer() in Python
    re.finditer() method in Python is used to search for all matches of a pattern in a string and return them as an iterator. In this article, we will explore about re.finditer() method in Python.Let's understand this with the help of an example:Pythonimport re text = "GFG, O, B, GFG, O" pattern = "GFG"
    2 min read
    re.compile() in Python
    The re.compile() method in Python is used to compile a regular expression pattern into a regex object. Compiling a pattern makes it more efficient when we need to use the same pattern several times, as it avoids re-compiling the pattern each time. Let’s look at how re.compile() works and when we sho
    3 min read
    Python | Pandas Series.str.findall()
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas str.findall() method is also used to find substrings or separators in each stri
    2 min read
    numpy.find() in Python
    numpy.core.defchararray.find(arr, substring, start=0, end=None): Finds the lowest index of the sub-string in the specified range. Parameters: arr : array-like or string to be searched. substring : substring to search for. start, end : [int, optional] Range to search in. Returns : An integer array wi
    1 min read
    re.match() in Python
    re.match method in Python is used to check if a given pattern matches the beginning of a string. It’s like searching for a word or pattern at the start of a sentence. For example, we can use re.match to check if a string starts with a certain word, number, or symbol. We can do pattern matching using
    2 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