Name

split

Synopsis

                        r.split(s,maxsplit=0)

Returns a list L of the splits of s by r (i.e., the substrings of s that are separated by non-overlapping, non-empty matches with r). For example, to eliminate all occurrences of substring 'hello' from a string, in any mix of lowercase and uppercase letters, one way is:

import re
rehello = re.compile(r'hello', re.IGNORECASE)
astring = ''.join(rehello.split(astring))

When r has n groups, n more items are interleaved in L between each pair of splits. Each of the n extra items is the substring of s matching r’s corresponding group in that match, or None if that group did not participate in the match. For example, here’s one way to remove whitespace only when it occurs between a colon and a digit:

import re
re_col_ws_dig = re.compile(r'(:)\s+(\d)')
astring = ''.join(re_col_ws_dig.split(astring))

If maxsplit is greater than 0, at most maxsplit splits are in L, each followed by n items as above, while the trailing substring of s after maxsplit matches of r, if any, is L’s last item. For example, to remove only the first occurrence of substring 'hello' rather than all of them, change the last statement in the first example above to:

astring = ''.join(rehello.split(astring, 1))

Get Python in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.