Python String rpartition() Method Examples

In the previous article, we have discussed Python String rjust() Method Examples
rpartition() Method in Python:

The rpartition() method looks for the last occurrence of a string and splits it into a tuple with three elements.

The part preceding the specified string is contained in the first element.

The specified string is contained in the second element.

The part following the string is contained in the third element.

Syntax:

string.rpartition(value)

Parameters

value: This is required. The string to look for.

If the specified value is not found, the rpartition() method returns a tuple containing the following values: 1 – an empty string, 2 – an empty string, 3 – the entire string.

For example:

gvn_str = "welcome to python learning platform"
rslt_tupl = gvn_str.rpartition("java")
print(rslt_tupl)

Output:

('', '', 'welcome to python learning platform')

Examples:

Example1:

Input:

Given string = "welcome to python learning platform, python programs"
Given value = "python"

Output:

The result tuple after applying rpartition() function on the given string : 
('welcome to python learning platform, ', 'python', ' programs')

Example2:

Input:

Given string = "good morning all"
Given value = "all"

Output:

The result tuple after applying rpartition() function on the given string : 
('good morning ', 'all', '')

String rpartition() Method Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Apply rpartition() function to the given string to search for the last occurrence of a string and splits it into a tuple with three elements as mentioned above.
  • Store it in another variable.
  • Print the result tuple after applying the rpartition() function on the given string for the given value.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "welcome to python learning platform, python programs"
# Give the value as static input and store it in another variable.
gvn_valu = "python"
# Apply rpartition() function to the given string to search for the last
# occurrence of a string and splits it into a tuple with three elements
# as mentioned above.
# Store it in another variable.
rslt_tupl = gvn_str.rpartition(gvn_valu)
# Print the result tuple after applying rpartition() function on the given string
# for the given value
print("The result tuple after applying rpartition() function on the given string : ")
print(rslt_tupl)
# Similarly, do the same for the other string and print the result string
gvn_str2 = "good morning all"
gvn_valu2 = "all"
rslt_tupl2 = gvn_str2.rpartition(gvn_valu2)
print("The result tuple after applying rpartition() function on the given second string : ")
print(rslt_tupl2)

Output:

The result tuple after applying rpartition() function on the given string : 
('welcome to python learning platform, ', 'python', ' programs')
The result tuple after applying rpartition() function on the given second string : 
('good morning ', 'all', '')

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Apply rpartition() function to the given string to search for the last occurrence of a string and splits it into a tuple with three elements as mentioned above.
  • Store it in another variable.
  • Print the result tuple after applying the rpartition() function on the given string for the given value.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Give the value as user input using the input() function and
# store it in another variable.
gvn_valu = input("Enter some random value = ")
# Apply rpartition() function to the given string to search for the last
# occurrence of a string and splits it into a tuple with three elements
# as mentioned above.
# Store it in another variable.
rslt_tupl = gvn_str.rpartition(gvn_valu)
# Print the result tuple after applying rpartition() function on the given string
# for the given value
print("The result tuple after applying rpartition() function on the given string : ")
print(rslt_tupl)
# Similarly, do the same for the other string and print the result string
gvn_str2 = input("Enter some random string = ")
gvn_valu2 = input("Enter some random value = ")
rslt_tupl2 = gvn_str2.rpartition(gvn_valu2)
print("The result tuple after applying rpartition() function on the given second string : ")
print(rslt_tupl2)

Output:

Enter some random string = hello this is btechgeeks hello
Enter some random value = hello
The result tuple after applying rpartition() function on the given string : 
('hello this is btechgeeks ', 'hello', '')
Enter some random string = good morning all this is python programming
Enter some random value = good
The result tuple after applying rpartition() function on the given second string : 
('', 'good', ' morning all this is python programming')

Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.