Python string rpartition() method

Python String rpartition() Method

Description

The rpartition() method is similar to the partition() method, except that the string is split at the last occurrence of the separator string.

Syntax

var.rpartition(sep)

Parameters

  • sep − The string to use as the separator.

Return Value

This method returns a tuple of three strings.

Example

var = 'Explicit is better than implicit'
var1 = var.rpartition('ci')
print ("Original string:", var)
print ("Partition result:", var1)

var = "Explicit is better than implicit"
var2 = var.rpartition('IS')
print ("Original string:", var)
print ("Partition result:", var2)

var = 'Explicit is better than implicit'
var3 = var.rpartition("")
print ("Original string:", var)
print ("Partition result:", var3)

Running this program produces the following output −

Original string: Explicit is better than implicit
Partition result: ('Explicit is better than implicit') impli', 'ci', 't')
Original string: Explicit is better than implicit
Partition result: ('', '', 'Explicit is better than implicit')
Traceback (most recent call last):
File "C:Usersmlathexamplesmain.py", line 12, in <module>
var3 = var.rpartition("")
^^^^^^^^^^^^^^^^^^^
ValueError: empty separator

Leave a Reply

Your email address will not be published. Required fields are marked *