English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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)
The rpartition() method takes a string parameter separator and splits it at the last occurrence.
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.
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")