Tutorial Study Image

Python rpartition()

The rpartition() function in python helps to divide the string as a tuple. It first finds out the last occurrence of the string and split into tuple having three parts. This first part contains part before the specified string, the second part contains specified string and the third part contains part after the given string.


string.rpartition(separator) #where separator can be numbers as well as symbols
 

rpartition() Parameters:

The rpartition() function takes single parameter. This method searches the last occurrence of the given string. The search in the partition() is case-sensitive.

Parameter Description Required / Optional
separator The string to search for Required

rpartition() Return Value

The return value is a tuple. If the separator parameter is not found then, two empty strings and the string itself are returned. If we are passing an empty string as a separator, then the method will throw ValueError.

Input Return Value
Integer Returns a Unicode character

 

Examples of rpartition() method in Python

Example 1: How rpartition() works if the seperator is not found?


string1 = "Python programming languages"

string2 = string1.rpartition("java")

print(string2)
 

Output:


('', '','Python programming languages')

Example 2: How rpartition() works if the seperator is found?


string1 = "Python programming languages"

string2 = string1.partition("programming")

print(string2)
 

Output:


('Python','programming','languages')

Example 3: How rpartition() works if the seperator is empty?


string1 = "Python programming languages"

string2 = string1.partition(" ")

print(string2)
 

Output:


('', '','Python programming languages')
ValueError: empty separator