Python String rpartition Method

The Python rpartition method divides a string at the final occurrence of a specified separator substring. It segments the string at the last instance of the provided parameter and returns a tuple. This tuple comprises three components: the segment preceding the separator, the separator itself, and the segment that follows the separator.

It yields an empty tuple containing only the separator if the separator is not present.

The method signature is given below.

Signature

Example

rpartition(sep)

Parameters

sep : A string argument that serves as a delimiter for the string.

Return

It returns a tuple, A 3-Tuple.

Let’s examine a few illustrations of the rpartition(sep) method to grasp its capabilities.

Python String rpartition Method Example 1

Let’s explore a straightforward application of the partition method across different scenarios.

Example

# Python rpartition() method example
# Variable declaration
str = "Python is a programming language"
# Calling function
str2 = str.rpartition("is")
# Displaying result
print(str2)
# seperator is at begining
str2 = str.rpartition("Python")
print(str2)
# seperator at ent
str2 = str.rpartition("language")
print(str2)
# when seperater is a substring
str2 = str.rpartition("av")
print(str2)

Output:

Output

('Python ', 'is', ' a programming language'')
('', 'Python', ' is a programming language')
('Java is a programming ', 'language', '')
('J', 'av', 'a is a programming language')

Python String partition Method Example 2

In the event that the separator is not located, the function will return a tuple that consists of the original string itself along with two empty strings positioned to the right. Refer to the example provided below.

Example

# Python rpartition() method example
# Variable declaration
str = "Python is a programming language"
# Calling function
str2 = str.rpartition("not")
# Displaying result
print(str2)

Output:

Output

('', '', 'Python is a programming language')

Input Required

This code uses input(). Please provide values below: