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 String replace()
Reading an HTTP Response Body as a String in Java
Python Program to Display Calendar
Python object()
Python Dictionary get()
Python if...else Statement
Python Set difference_update()
Python delattr()
Python Iterators
Python Function Arguments
Python String rjust()
Python Variables, Constants and Literals
Python Program to Find the Largest Among Three Numbers
Java – String to Reader
Python Numbers, Type Conversion and Mathematics
Python Dictionary popitem()
Python String lower()
Python Custom Exceptions
Python List
Python tuple()
Python Set issubset()
Python Program to Remove Duplicate Element From a List
Python print()
Python Program to Check If a String Is a Number (Float)
Python Dictionary pop()
Python Program to Measure the Elapsed Time in Python
Python Keywords and Identifiers
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Check if a String is a Palindrome in Java
Python String isnumeric()
Python strptime()
Python String splitlines()