Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python rpartition

by suresh

Python rpartition is one of the Python String Method, used to partition the given string using the specified separator and return a tuple with three arguments.

This Python rpartition function starts looking for the separator from the Right-Hand side. Once rpartition finds the separator, it returns 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 section, we discuss how to write rpartition in Python Programming with example.

Syntax of a rpartition Function in Python

The syntax of the string rpartition method in Python Programming Language is

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

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

Return Value

Python rpartition function returns Tuple with three arguments. For example, If we have A*B*C and If we use * as a separator, rpartition function searches for * from right to left. Once it finds * symbol, it returns 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, the rpartition function looks for the first occurrence from the right side.

Python rpartition method Example

The following set of examples helps to understand the rpartition Function in Python Programming Language.

# 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

The following Python rpartition statement partitions the Str1 string into multiple parts based on the separator we specified. That is Empty Space and prints the output.

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

It partitions the Str2 string into multiple parts based on the ‘-‘ symbol and prints the output.

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

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

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

Here, we used the rpartition 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)

The above Python statement looks for @, which doesn’t exist. So, it returns the Tuple as (”, ”, ‘Tutorial Gateway’)

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

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python Pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

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

Home | About Us | Contact Us | Privacy Policy