Home / Python / Reference / String methods

Python string partition() method

Split at the first separator into exactly three parts: before, separator, after.

Syntax

str.partition(sep)

If the separator is missing you get (whole string, "", ""), never an error.

Parameters

septhe separator

Return value

a tuple of three strings

Example

Code

print("key=value=more".partition("="))
key, _, value = "timeout=30".partition("=")
print(key, value)
print("no separator".partition("="))

Output

('key', '=', 'value=more')
timeout 30
('no separator', '', '')

Related

Learn it step by step in the free lesson Strings, with exercises that check your answers.