In this tutorial, we’ll learn what Python string rpartition() method is and how to properly use it. We’ll go through multiple Python code examples to understand how rpartition method works.
Introduction: Python String rpartition() Method
This method returns a tuple after splitting the string at last occurence of the string argument. The tuple includes string before argument, the argument itself and string after argument.
Python string rpartition method return a tuple having the original string with two empty strings at the beginning(if specified string argument is not found).
Syntax of rpartition() Method
stringVal.rpartition(stringArg(required))
- Python string rpartition method takes a string argument. This argument is required.
- It returns a tuple which includes the string before argument, the argument itself and a string after argument. Two empty strings at the beginning(if argument string is not found).
- Python string rpartition method takes the occurence of specified string argument from right(or last occurence of string argument).
Example 1: rpartition() Method applied on a Python String
print('Python Programming'.rpartition('Python')) print('This is Python Programming Python'.rpartition('Python')) print('This is Python Programming Language'.rpartition('Flutter')) print('This is Python Programming Python Language'.rpartition('Python')) print('This is Python Programming Programming Language'.rpartition('Program'))
Output
('', 'Python', ' Programming') ('This is Python Programming ', 'Python', '') // last occurence of specified argument string is used ('', '', 'This is Python Programming Language') // the specified string argument is not found ('This is Python Programming ', 'Python', ' Language') ('This is Python Programming ', 'Program', 'ming Language')
This is how Python string rpartition method works. We can see that it takes the occurence of specified string argument from right(or last occurence of string argument).
You can try it with even more examples to better understand the working of this method.
Example 2: Python String rpartition() Method Return Value
val= 'This is Python Programming Python Language' returnedVal=val.rpartition('Pyth') print(returnedVal)
Output
('This is Python Programming ', 'Pyth', 'on Language')
We can see that Python string rpartition method has returned a tuple which includes a string before argument, the argument itself and a string after the specified argument.
Conclusion
To conclude this tutorial, hope you now have a detailed practical understanding of how to properly use Python string rpartition method. I’d be very happy to receive your feedback on this post. Thank you for reading it.