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




partition( ) FUNCTION


partition( ) 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.


Example :


x = "Hello Beautiful World"
y = x.partition("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.partition("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.partition("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.partition("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 first element. And the second and third elements would vacant.


java_Collections

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



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

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