Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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.sub() - Python RegEx
Next article icon

Python - Regex split()

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

re.split() method in Python is generally used to split a string by a specified pattern. Its working is similar to the standard split() function but adds more functionality. Let’s start with a simple example of re.split() method:

Python
import re  s = "Geeks,for,Geeks"  # Using re.split() to split the string result = re.split(r',', s)  print(result) 

Output
['Geeks', 'for', 'Geeks'] 

Syntax of re.split() method

re.split(pattern, string, maxsplit=0, flags=0)

Parameters:

  • pattern (required): This regular expression pattern that defines where the string should be split.
  • string (required): This input string to be split based on the given pattern.
  • maxsplit (optional, default is 0): This maximum number of splits to perform; 0 means no limit.

Return Type: This re.split() method returns a list of strings.

Examples of python regex split() function

Splitting a String by Comma in Python

re.split() function from the re module is used when you need more flexibility, such as handling multiple spaces or other delimiters.

Python
import re s = "Geeks for Geeks"  # Regular expression to match one or more non-word characters pattern = r'\W+'   # Split the string using the pattern words = re.split(pattern, s)  print(words) 

Output
['Geeks', 'for', 'Geeks'] 

Explanation:

  • This regex \W+ splits the string at non-word characters.
  • Since there are none in the sentence, it returns a list of words.

Splitting Strings with a Maximum Number of Splits

We can use re.split() from the re module to split a string with a regular expression and specify a maximum number of splits using the maxsplit parameter.

Python
import re  s = "Geeks for Geeks"  #Using re.split() to split the string by spaces with a maximum of 2 splits result = re.split(r' ', s, maxsplit=2) print(result) 

Output
['Geeks', 'for', 'Geeks'] 

Explanation:

  • This splits the string s at spaces, limiting the splits to a maximum of 2.

Splitting a String with Capturing Groups

This splits the string at the specified delimiters (comma and semicolon), but because of the capturing group, the delimiters themselves are retained in the output list.

Python
import re s = "Geeks, for; Geeks"  # Using split() with a capturing group to keep the delimiters result = re.split(r'(,|;)', s)  print(result) 

Output
['Geeks', ',', ' for', ';', ' Geeks'] 

Next Article
re.sub() - Python RegEx

V

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

Similar Reads

  • Python RegEx
    In this tutorial, you'll learn about RegEx and understand various regular expressions. Regular ExpressionsWhy Regular ExpressionsBasic Regular ExpressionsMore Regular ExpressionsCompiled Regular Expressions A RegEx is a powerful tool for matching text, based on a pre-defined pattern. It can detect t
    9 min read
  • re.sub() - Python RegEx
    re.sub() method in Python parts of a string that match a given regular expression pattern with a new substring. This method provides a powerful way to modify strings by replacing specific patterns, which is useful in many real-life tasks like text processing or data cleaning. [GFGTABS] Python import
    2 min read
  • Regex Cheat Sheet - Python
    Regex or Regular Expressions are an important part of Python Programming or any other Programming Language. It is used for searching and even replacing the specified text pattern. In the regular expression, a set of characters together form the search pattern. It is also known as the reg-ex pattern.
    9 min read
  • Verbose in Python Regex
    In this article, we will learn about VERBOSE flag of the re package and how to use it. re.VERBOSE : This flag allows you to write regular expressions that look nicer and are more readable by allowing you to visually separate logical sections of the pattern and add comments. Whitespace within the pat
    3 min read
  • Python String split()
    Python String split() method splits a string into a list of strings after breaking the given string by the specified separator. Example: [GFGTABS] Python string = "one,two,three" words = string.split(',') print(words) [/GFGTABS]Output: ['one', 'two', 'three']Python String split() M
    6 min read
  • re.subn() in Python
    re.subn() method in Python is used to search for a pattern in a string and replace it with a new substring. It not only performs the replacement but also tells us how many times the replacement was made. We can use this method when we need to replace patterns or regular expressions in text and get a
    3 min read
  • Python NLTK | tokenize.regexp()
    With the help of NLTK tokenize.regexp() module, we are able to extract the tokens from string by using regular expression with RegexpTokenizer() method. Syntax : tokenize.RegexpTokenizer() Return : Return array of tokens using regular expression Example #1 : In this example we are using RegexpTokeni
    1 min read
  • How to Import Regex in Python
    Regex is a built-in library in Python. You can use the re module to work with regular expressions. Here are some basic examples of how to use the re module in Python: Examples of Regular Expression in PythonWe can import it in our Python by writing the following import statement in the Python script
    2 min read
  • re.search() in Python
    re.search() method in Python helps to find patterns in strings. It scans through the entire string and returns the first match it finds. This method is part of Python's re-module, which allows us to work with regular expressions (regex) simply. Example: [GFGTABS] Python import re s = "Hello, we
    3 min read
  • Python - Regex Lookbehind
    Regex Lookbehind is used as an assertion in Python regular expressions(re) to determine success or failure whether the pattern is behind i.e to the right of the parser's current position. They don't match anything. Hence, Regex Lookbehind and lookahead are termed as a zero-width assertion. In this p
    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