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
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 |
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 |
string1 = "Python programming languages"
string2 = string1.rpartition("java")
print(string2)
Output:
('', '','Python programming languages')
string1 = "Python programming languages"
string2 = string1.partition("programming")
print(string2)
Output:
('Python','programming','languages')
string1 = "Python programming languages"
string2 = string1.partition(" ")
print(string2)
Output:
('', '','Python programming languages') ValueError: empty separator