English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operation

Python objects and classes

Python date and time

Advanced knowledge of Python

Python Reference Manual

Python string rpartition() usage and example

Python string methods

The rpartition() method is similar to the partition() method, but it searches for the delimiter from the end of the target string, that is, from the right side.
If the string contains the specified delimiter, it returns a3A tuple of tuples, the first of which is the substring to the left of the separator, the second is the separator itself, and the third is the substring to the right of the separator.

The syntax of rpartition() is:

string.rpartition(separator)

rpartition() parameters

The rpartition() method takes a string parameter separator and splits it at the last occurrence.

rpartition() return value

The rpartition() method searches for the last occurrence of the specified string and splits the string into a tuple containing three elements.
The first element contains the part of the string before the specified string.

The second element contains the specified string.

The third element contains the part of the string after it.

Example: How does rpartition() work?

string = "Python is fun"
# Found delimiter 'is '
print(string.rpartition('is '))
# 'not' delimiter not found
print(string.rpartition('not '))
string = "Python is fun, isn't it"
# Split at the last occurrence of 'is'
print(string.rpartition('is'))

When running the program, the output is:

('Python', 'is', 'fun')
('', '', 'Python is fun')
('Python is fun, ', 'is', "n't it")

Python string methods