Published Jul 30, 2021Updated Jun 10, 2022
Contribute to Docs
.split() method of the
re module divides a string into substrings at each occurrence of the specified character(s). This method is a good alternative to the default
.split() string method for instances that require matching multiple characters.
re.split(<pattern>, string, <maxsplit>, <flags>)
<pattern> is a regular expression that can include any of the following:
- A string:
- A character class code:
- A regex symbol:
The other arguments include:
- An integer for the maximum number of splits (max split):
The following example splits the text at each parenthesis:
import rerace_info = '''The top three race finishers were: Jane Smith (2:14), Sarah Long (2:18) and Suzy Reynolds (2:20).'''re.split(r"\(|\)", race_info)# A backslash indicates that the parentheses are part of the pattern
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
Computer ScienceLooking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
Includes 6 Courses
With Professional Certification