What is the partition() method in Python?

The partition() method in Python cuts a string into three parts with a delimiter.

Syntax

 `string.partition(delimiter)` 

This method will only search the first occurrence of the delimiter.

Return value

This method will return a tuple with three values: the string before the delimiter, the delimiter itself, and the string after it.

If it doesn’t find the delimiter, it will return a tuple with only the string as the first element.

Example

print("educative is awesome!".partition("is "))
print("educative is awesome!".partition("hello"))

Free Resources