Python Glossary Русский

all Python 3Strings (str)function

str.partition

splitting

Split at the first occurrence of the separator into a (head, sep, tail) tuple.

Syntax

s.partition(sep)

The win over split() is that the result always has exactly three items, so head, sep, tail = s.partition('=') never raises ValueError, whatever the input. Detect a missing separator by the empty middle item rather than by an empty tail — when nothing matches, the whole string lands in the head. If you need the last separator instead of the first, use rpartition().

Examples

print('a=b=c'.partition('='))  # → ('a', '=', 'b=c')
print('abc'.partition('='))    # → ('abc', '', '')
print('k: v'.partition(': '))  # → ('k', ': ', 'v')

See also

Open in the full glossarydocs.python.orgReport an inaccuracy