Python string Method - rpartition()

The rpartition() method is a built-in string method in Python that splits a string into three parts based on the last occurrence of a specified separator. The method returns a tuple containing the three parts.

The syntax for using rpartition() method is as follows:

refer t‮figi:o‬tidea.com
string.rpartition(separator)

Here, string is the original string, and separator is the substring to use as the separator.

The method returns a tuple containing three elements: the substring before the last occurrence of the separator, the separator itself, and the substring after the separator. If the separator is not found in the string, the method returns a tuple containing the original string and two empty strings.

Here's an example of using the rpartition() method:

string = "Hello, World!"
parts = string.rpartition(",")
print(parts)

Output:

('Hello', ',', ' World!')

In the example above, the rpartition() method was used to split the original string "Hello, World!" based on the last occurrence of the comma separator (","). The resulting tuple is stored in the variable parts, which contains the three parts of the string: "Hello", ",", and " World!".