Python Function Reference

str.partition

Split the string into three parts at the first occurrence of a separator.

Python 3.13
str.partition(sep)

Parameters

sepstr
The separator; it cannot be an empty string.

Return value

A tuple (before the separator, the separator, after the separator); (string, "", "") if the separator is missing.

Examples

Splitting at the first = sign

Python 3.13
print('key=value=1'.partition('='))
('key', '=', 'value=1')

Separator not found

Python 3.13
print('hello'.partition('='))
('hello', '', '')

Details

Unlike str.split, it always returns exactly three parts, so the result can be unpacked into variables right away: key, sep, value = line.partition("="). To search for the separator from the end, use str.rpartition.

See also