.partition()

Published Jun 9, 2021Updated Oct 13, 2023
Contribute to Docs

Searches a string for a given keyword and splits that string into a three part tuple.

Syntax

string.partition("keyword")

Example

The .partition() method searches for an exact match of the given keyword:

my_string = "Do not go gentle into that good night"
my_tuple = my_string.partition("gentle")
print(my_tuple)
# Output: ('Do not go ', 'gentle', ' into that good night')

No Match Found

If no match is found, .partition() will return a tuple with the full string, followed by two empty strings

Code
Output
Loading...

Multiple Matches

If there are multiple matches, the first one will be used:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy