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:
APIs in Node.js vs Python - A Comparison
Python Program to Create a Long Multiline String
Encode a String to UTF-8 in Java
Python Operators
Python Dictionary setdefault()
Python iter()
Python Program to Shuffle Deck of Cards
Python Program to Access Index of a List Using for Loop
Convert Character Array to String in Java
Python dict()
Python Program to Count the Number of Each Vowel
Python List pop()
Python help()
Python Program to Check If a List is Empty
Python Program to Solve Quadratic Equation
Python Program to Transpose a Matrix
Python Program to Get the File Name From the File Path
How to get current date and time in Python?
Python memoryview()
Python Exception Handling Using try, except and finally statement
Python Program to Find Armstrong Number in an Interval
Count Occurrences of a Char in a String
Python Program to Find ASCII Value of Character
Python str()
Python bytes()
Python Program to Convert Celsius To Fahrenheit
Python Function Arguments
Python super()
Python issubclass()
Most commonly used String methods in Java
Converting String to Stream of chars
Python Statement, Indentation and Comments