In this section, we will learn what String rpartition() method is and how to use it in Python.
What is String rpartition() Method in Python?
The Python string partition() method, as the name of the method suggests, is used to partition a string value.
Basically, this method takes a string value as its argument and then looks for the last occurrence of this value in the target string (the one that invoked the method). Now when it found this value, it will return a tuple that contains 3 items:
- The first item is the sub-string of the target string that comes before the specified value (the argument of the method).
- The second item is the specified string value (The argument).
- The third item is the sub-string of the string value that comes after the specified value (the argument of the method).
Python String rpartition() Method Syntax:
String rpartition() Method Parameter
The method takes one argument and that is the string value we want to look for its last occurrence in the target string.
String rpartition() Method Return Value
The return value of this method is a tuple with 3 items that mentioned at the beginning of this section.
Note: if the target string value didn’t have the specified value (the argument of the method) then the tuple that returned from this method will have the entire string value as its first item and the second and third items will be only an empty string.
Example: using python string rpartition() method
s1 = "My name is John Doe and my cousin's name is also the same as mine! " print(s1.rpartition("name"))
("My name is John Doe and my cousin's ", 'name', ' is also the same as mine! ')