Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




rpartition( ) FUNCTION


rpartition( ) Function


The partition( ) Function is used to search for a particular substring mentioned in it and creates a tuple that has three values.


The First value is the one that resides on the left side of the search substring.


The second value is the searched substring itself.


And the third value is the one that resides on the right side of the search substring.


Note : The only difference between 'rpartition( )' function and 'rpartition( )' function is when the searched substring is not found, the 'rpartition( )' function creates a tuple with the entire String as the third value(Which we will see later).

Example :


x = "Hello Beautiful World"
y = x.rpartition("Beautiful")
print(y)


Output :



  ('Hello ', 'Beautiful', ' World')

In the above code, we have declared a string 'Hello Beautiful World'. And assigned it to a variable 'x'.


x = "Hello Beautiful World"

java_Collections

Then we have used the 'x.partition("Beautiful")' function to search for the substring "Beautiful" and create a Tuple with three values.


  1. So that the first value would be the one on the left side of the searched substring 'Beautiful'.Which is 'Hello'.
  2. The second value is the searched substring itself. Which is 'Beautiful'.
  3. And the third value is the one on the right side of the searched substring 'Beautiful'.Which is 'World'.

y = x.rpartition("Beautiful")

java_Collections

Now if you look at the output, a tuple with three values is formed.


Output :



  ('Hello ', 'Beautiful', ' World')

Note : Values within brackets '( )' represents a tuple.

Now, let us say, we have specified a value in the 'partition( )' Function that is not found.


Example :


x = "Hello Beautiful World"
y = x.rpartition("Wonderful")
print(y)


Output :



  ('', '', 'Hello Beautiful World')

In the above code, we have declared a string 'Hello Beautiful World'. And assigned it to a variable 'x'.


x = "Hello Beautiful World"

java_Collections

Then we have used the 'x.rpartition("Wonderful")' function to search for the substring "Wonderful" and create a Tuple with three values.


y = x.partition("Wonderful")

But the substring 'Wonderful' is not present in the String 'Hello Beautiful World'. In such case, the elements of the tuple would be the complete String 'Hello Beautiful World' itself as the last element. And the first and second elements would vacant.


java_Collections

Now if you look at the output, a tuple with three values is formed.


Output :



  ('', '', 'Hello Beautiful World')

Note : Values within brackets '( )' represents a tuple.