Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Python RPartition

by suresh

RPartition

Python RPartition is one of the Python String Method which is used to partition the given string using the specified separator and return a tuple with three arguments. This function will start looking for the separator from Right Hand side and once it finds the separator, it will return the string before the Separator as Tuple Item 1, Separator itself as Tuple Item 2 and string after the Separator (or remaining string) as Tuple Item 3. In this article we will show you, How to write rpartition in Python Programming with example.

TIP: Please refer Python Partition article to understand the Partition method, which performs the same operation but from left to right.

Syntax of a RPartition Function in Python

The basic syntax of the string rpartition method in Python Programming Language is as shown below:

String_Value.rpartition(Separator)
  • String_Value: Please select the valid String variable or you can use the String directly.
  • Separator: This argument is required and if you forget this argument, python will throw TypeError.

NOTE: If you pass the non existing item as the separator then rpartition will return two empty strings as Tuple Item 1, Item 2 followed by the whole string as Tuple Item 3.

Return Value

Python rpartition function will return Tuple with three arguments. For example, If we have A*B*C and If we use * as separator, rpartition function will search for * from right to left. Once it find * symbol, it will return the string before the * symbol as Tuple Item 1 (A*B), * as Tuple Item 2 and remaining string as Tuple Item 3 (C)

TIP: Even though there are multiple occurrences of the separator, rpartition function will look for the first occurrence from the right side.

Python rpartition method Example

The following set of examples will help you understand the rpartition Function in Python Programming Language.

PYTHON CODE

# Python rpartition Method Example
 
Str1 = 'Tutorial Gateway Website'
Str2 = 'Free-Tutorials-On-Python-Language'
Str3 = '[email protected]@mailaccount.com'
 
Str4 = Str1.rpartition(' ')
print("Right Partitioning String 1 = ", Str4)

Str5 = Str2.rpartition('-')
print("Right Partitioning String 2 = ", Str5)

Str6 = Str3.rpartition('@')
print("Right Partitioning String 3 = ", Str6)

#Performing Python rpartition function directly
Str7 = 'Python.Images.png'.rpartition('.')
print("Right Partitioning String 7 = ", Str7)

# Non Existing Item
Str8 = 'Tutorial Gateway'.rpartition('@')
print("Right Partitioning String 8 = ", Str8)

OUTPUT

Python RPartition Method

ANALYSIS

First we declared three String variable Str1, Str2, Str3 and assigned corresponding value using following statement

Str1 = 'Tutorial Gateway Website'
Str2 = 'Free-Tutorials-On-Python-Language'
Str3 = '[email protected]@mailaccount.com'

Following statement will partition the Str1 string into multiple parts based on the separator we specified (i.e, Empty Space) and prints the output.

Str4 = Str1.rpartition(' ')
print("Right Partitioning String 1 = ", Str4)

Following statement will partition the Str2 string into multiple parts based on the ‘-‘ symbol and prints the output.

Str5 = Str2.rpartition('-')
print("Right Partitioning String 2 = ", Str5)

Following statement will partition the Str3 string into multiple parts based on the ‘@’ symbol and prints the output.

Str6 = Str3.rpartition('@')
print("Right Partitioning String 3 = ", Str6)

Follwoing statement we used the Partition function directly on String

Str7 = 'Python.Images.png'.rpartition('.')
print("Right Partitioning String 7 = ", Str7)

If you try something like,

Str8 = 'Tutorial Gateway'.rpartition('@')
print("Right Partitioning String 8 = ", Str8)

Above statement will look for @, which doesn’t exist so it will return the tuple as (”, ”, ‘Tutorial Gateway’)

Thank You for Visiting Our Blog

Placed Under: Python

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy