Table of Contents
The rpartition() splits the string at the last occurrence of the argument string and returns a tuple containing the part the before separator, argument string and the part after the separator.
The syntax of rpartition() is:
string.rpartition(separator)
1. rpartition() Parameters()
rpartition() method takes a string parameter separator that separates the string at the last occurrence of it.
2. Return Value from rpartition()
rpartition() method returns a 3-tuple containing:
- the part before the separator, separator parameter, and the part after the separator if the separator parameter is found in the string
- two empty strings, followed by the string itself if the separator parameter is not found
3. Example: How rpartition() works?
string = "Python is fun"
# 'is' separator is found
print(string.rpartition('is '))
# 'not' separator is not found
print(string.rpartition('not '))
string = "Python is fun, isn't it"
# splits at last occurence of 'is'
print(string.rpartition('is'))
Output
('Python ', 'is ', 'fun')
('', '', 'Python is fun')
('Python is fun, ', 'is', "n't it")
Related posts:
Python Get Current time
Python Program to Find the Factorial of a Number
Python String partition()
Python list()
Python Program to Compute the Power of a Number
Python @property decorator
Python Statement, Indentation and Comments
Python del Statement
Python Program to Find Sum of Natural Numbers Using Recursion
Python eval()
Python Program to Find the Size (Resolution) of a Image
Python Dictionary fromkeys()
Python Dictionary clear()
Python Program to Trim Whitespace From a String
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Java – Generate Random String
Python String isalpha()
String Hashing
Python Program to Parse a String to a Float or Int
Python max()
Python Dictionary keys()
Python Program to Check Leap Year
Python frozenset()
Python String split()
Python __import__()
Python Program to Find Numbers Divisible by Another Number
Python Set intersection()
Python Set isdisjoint()
Python Program to Sort a Dictionary by Value
Node.js vs Python for Backend Development
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Check if a Number is Positive, Negative or 0