liveBook · Manning

Split

As explained in Python How-To

Split is a regular-expression method that divides a string at occurrences of matched patterns, optionally limiting the number of divisions.

Use regular-expression split to divide a string wherever a pattern matches. Unlike basic string splitting, the separator can describe multiple matching forms, and an optional maximum limits the number of splits. section 2.4.5 section 2.3.3

Split at each run of digits

import re parts = re.split(r"\d+", "a1b2c3d4e") print(parts) # ['a', 'b', 'c', 'd', 'e'] The pattern r"\d+" matches one or more digits, so every matching run becomes a division point. section 2.4.5

Choose the splitting approach according to the input:

  • Use basic str.split when the data has one known separator, such as commas between task fields. section 2.3.3
  • Use regular-expression split when the boundary is a pattern rather than one literal separator. section 2.4.5
  • Set a maximum split count when only an initial portion of the string should be divided; compare split and rsplit when direction matters. section 2.3.5 section 2.3.4
Table 2.3 Common regular expression methods (view table figure)

Method

Code example

Match/return value

search: Returns a Match if a match is found anywhere in the string.

re.search(r"\d+", "ab12xy")

'12'

re.search(r"\d+", "abxy")

None

match: Returns a Match only if a match is found at the string’s beginning.

re.match(r"\d+", "ab12xy")

None

re.match(r"\d+", "12abxy")

'12'

findall: Returns a list of strings that match the pattern. When the pattern has multiple groups, the item is a tuple.

re.findall(r"h[ie]\w", "hi hey hello")

['hey', 'hel']

re.findall(r"(h|H)(i|e)", "Hey hello")

[('H', 'e'), ('h', 'e')]

finditer: Returns an iterator3 that yields the Match objects.

re.finditer(r"(h|H)(i|e)", "hi Hey hello")

An iterator

split: Splits the string by the pattern.

re.split(r"\d+", 'a1b2c3d4e')

['a', 'b', 'c', 'd', 'e']

sub: Creates a string by replacing the matched with the replacement.

re.sub(r"\D", "-", '123,456_789')

'123-456-789'

Compare split with search, match, findall, finditer, and sub through representative regular-expression calls and return values. section 2.4.5

How Python How-To covers it

How deep it goes
Defines pattern-based splitting, optional maximum splits, and contrasts it with literal splitting and rsplit.
Examples
Uses digit-pattern splitting, comma-separated task records, and comparisons between split and rsplit.
What it assumes
Builds from basic string splitting and requires constructing a regular-expression pattern for the intended boundary.
What you can do after
Apply it via Python regular-expression calls to separate text at flexible patterns and optionally cap the divisions.
How it connects
Connects regular-expression split with string splitting, rsplit, search, match, findall, finditer, and substitution.

Also explained in

Compare how 11 books explain split →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.