Python String rpartition Method - Python Tutorial | Logic Practice
Python Course / Other / Python String rpartition Method

Python String rpartition Method

BLUF: This lesson on Python String rpartition Method provides a comprehensive guide to understanding and implementing this concept in Python. Whether you're a beginner or looking to refresh your knowledge, you'll find clear explanations and interactive code examples here.
Key Concept: Python String rpartition Method

Mastering Python String rpartition Method is essential for building efficient Python applications. Focus on the syntax and the best practices highlighted in this tutorial.

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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience