< Prev
Next >

Python - String rpartition() Method

Contrary to the partition string method, which starts searching for a specific value at the left of a string in order to partition it, the rpartition() string method is starts searching for a specific value at the right of the string in order to perform its partition.

If the searched value is found, the rpartition() method returns a tuple of three split values, where -

Signature of rpartition() method


Parameters Description
This is the value to be searched in the invoked string and on the basis of it a tuple of three values will be created, only if this value is found.

Even when the value to be searched is having multiple occurrences within the invoked string, but the rpartition() method partitions the string only at the first occurrence of the searched value and the rest of occurrences of the searched value are ignored.

Note : The rpartition() method begins its search at the left of the invoked string. Let us see an example.

# Python - Example of rpartition() method.

# Creating a string 
s1 = 'Eddie edited a diary!'

# Printing the value of string 
print('String is : ', s1)

# Calling the rpartition() method on string
# which will perform the number of splits on the basis of searched value  i.e. di
# and returns a tuple containing all the split values
tuple1 = s1.rpartition('di')

# Priting all the split values 
print('A tuple split by di : ', tuple1)

Output is :

String is :  Eddie edited a diary!
A tuple split by di :  ('Eddie edited a ', 'di', 'ary!')

As you can see in the example, even though the string had 3 occurrences of the searched value ed but still the rpartition() method partitioned the string at the first occurrence of the searched value ed.

Note : Using the rpartition() search has begun from the left of the string.

Please share this article -

< Prev
Next >
< String partition() Method
String zfill() Method >


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition