.partition()

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...

Mulitple Matches

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

Code
Output
Loading...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Python on Codecademy

Contributors