![]() |
VOOZH | about |
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:
['Geeks', 'for', 'Geeks']
re.split(pattern, string, maxsplit=0, flags=0)
Parameters:
0 means no limit.Return Type: This re.split() method returns a list of strings.
re.split() function from the re module is used when you need more flexibility, such as handling multiple spaces or other delimiters.
['Geeks', 'for', 'Geeks']
Explanation:
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.
['Geeks', 'for', 'Geeks']
Explanation:
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.
['Geeks', ',', ' for', ';', ' Geeks']