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:
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Program to Shuffle Deck of Cards
Python filter()
Python setattr()
Python Global Keyword
Python dict()
Python int()
Python *args and **kwargs
Jackson – Marshall String to JsonNode
Python ascii()
Adding a Newline Character to a String in Java
Python Program to Find All File with .txt Extension Present Inside a Directory
Python pass statement
Python Program to Get the Last Element of the List
Deep Learning with Python - Francois Chollet
Python Set discard()
Python Program to Copy a File
Convert String to Byte Array and Reverse in Java
Python Program to Convert String to Datetime
Python Set copy()
Python String lstrip()
Python Decorators
Converting a List to String in Java
Python Program to Check If a String Is a Number (Float)
Python ord()
Python Set issubset()
Python vars()
Python File I/O Operation
Python String partition()
Python sorted()
Python len()
Python Program to Count the Number of Occurrence of a Character in String