Methods and Functions

Python String rpartition()

Python’s string methods have different functionalities. By using these built-in string module functions we can perform specific operations on the string iterables. One such operation is string partition. Python rpartition() function lets us separate the string passed as an argument to the function by specifying the separator value. This article focuses on Python rpartition(), its applications, and few basic examples to understand more about the Python rpartition().

Python rpartition() function

Definition

  • Python rpartition() function is used to partition a string at the last occurrence of the given string and return a tuple that includes 3 parts – the part before the separator, the argument string (separator itself), and the part after the separator.
  • The Python rpartition() string method searches for the specified separator substring and returns a tuple with 3 strings namely – everything before the separator, the separator itself, and everything after it.

rpartition() Syntax

The Python rpartition string function divides a given string using the provided separator and returns a tuple with three arguments. The syntax for Python string rpartition() is as follows:

                    

string.rpartition(separator)

rpartition() Parameters

The rpartition() function accepts a single parameter:

separator – a string parameter that separates the string at the last occurrence of it.

Note – If the separator argument is kept empty, then the Python interpreter will throw a TypeError exception.

Return value from rpartition()

The rpartition() string method returns a 3-tuple containing the following:

  • If the separator parameter is found in the string, the portion before the separator, the separator parameter, and the part after the separator is returned.
  • If the separator parameter is not found, then a tuple with 2 empty strings, followed by the string itself is returned.

For example, if we have A*B*C as our string input and we use * as a separator, the rpartition() function looks for * from right to left. When it discovers the * symbol, it returns the string preceding the * symbol as Tuple Item 1 (A*B), and the * symbol as Tuple Item 2. And the remainder of the string as Tuple Item 3 (C).

Example 1: How rpartition() works?

Example

                    

# Python program to illustrate rpartition()
string = 'Hello my name is Jimmy'

# when separator is found
print(string.rpartition('is'))

# when separator is not found
print(string.rpartition('who'))

string2 = 'Cricket is fun to watch, isnt it true?'
# when separator occurs multiple times in the string
print(string2.rpartition('is'))

Output

                    

('Hello my name ', 'is', ' Jimmy')
('', '', 'Hello my name is Jimmy')
('Cricket is fun to watch, ', 'is', 'nt it true?')

If a string has two occurrences of the separator string, it is divided at the last occurrence, i.e. it searches for the separator from the Right-Hand side. This can be seen in the above example, where string2.rpartition(‘is’) splits from the last occurrence of ‘is’.

Example 2: Empty separator in rpartition()

Example

                    

mystr = 'Welcome back Jimmy'
print(mystr.rpartition())

Output

                    

Traceback (most recent call last):
File "", line 2, in 
TypeError: rpartition() takes exactly one argument (0 given)

Difference between partition() and rpartition()

The string module has various operative functions. These being Python’s partition() and rpartition(). Now, what’s the main difference between these two? They both function in a similar manner. The only difference is that the partition() method splits the string from the first occurrence of the separator, while the rpartition() separates the string from the last occurrence of the separator.

Example

                    

mystr = 'Hello World'

print(mystr.partition('o'))
print(mystr.rpartition('o'))

Output

                    

('Hell', 'o', ' World')
('Hello W', 'o', 'rld')

Frequently Asked Questions

Q1. What is rpartition in Python?

Python rpartition() function lets us separate the string passed as an argument to the function by specifying the separator value. The Python rpartition() string method searches for the specified separator substring and returns a tuple with 3 strings namely – everything before the separator, the separator itself, and everything after it.

Q2. What does rstrip() do in Python?

The rstrip() function in Python eliminates all trailing characters from a string. It indicates that it removes all of the specified characters from the string’s right side. If the parameter is not specified, it removes all whitespaces from the string. The rstrip() method returns a string copy with the trailing characters deleted (based on the string argument passed).

Example

                    

string = 'Python Programming'
print(string.rstrip('ing'))

Output

                    

Python Programm

Q3. How do I use rsplit in Python?

The rsplit() method splits a string from the right at the separator supplied and returns a list of strings. If no separator is supplied, any whitespace string is used.

Example

                    

string = 'Python is a Programming language'
print(string.rsplit(' '))

Output

                    

['Python', 'is', 'a', 'Programming', 'language']

Q4. How do I translate in Python?

Python provides a string translate() function that is used to return each character in the string that is mapped to its matching character in the translation table. This translation table is created with the help of maketrans() method.

Example

                    

string = 'Python Programming'
trans_dict = {'P': '1', 'o': '2', 'm': '3'}

mytable = string.maketrans(trans_dict)
translation = string.translate(mytable)

print('Original String:', string)
print('Translated String:', translation)

Output

                    

Original String: Python Programming
Translated String: 1yth2n 1r2gra33ing

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.