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 Program to Find the Factorial of a Number
Python all()
Python Dictionary items()
Python for Loop
How to get current date and time in Python?
Python Program to Return Multiple Values From a Function
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Find Hash of File
Python String rindex()
Python bin()
Converting String to Stream of chars
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python break and continue
Format ZonedDateTime to String
Python Program to Make a Flattened List from Nested List
Python List index()
Python bytearray()
Deep Learning with Python - Francois Cholletf
Encode a String to UTF-8 in Java
Python hex()
Python List reverse()
Python Operator Overloading
Python iter()
Python Program to Differentiate Between type() and isinstance()
Convert char to String in Java
Python round()
Python dir()
Python String islower()
Python Program to Get a Substring of a String
Converting a List to String in Java
Deep Learning with Applications Using Python - Navin Kumar Manaswi
String Set Queries