Menu Close

Python string rpartition() method

Python String rpartition

In this article, you will learn about the Python string rpartition() method. rpartition function in Python search for the last occurrence of the specified string.

In the previous tutorial, we have seen Python string rindex().In this guide, we will see all about the Python string rpartition method along with examples so that you can easily understand it.

Python string rpartition() method

Python string rpartition method is a string built-in method that is used to search for the last occurrence of a specified string and split the string into a tuple containing three parts.

  • The first element contains the part before the specified string.
  • The second part contains the specified string.
  • The last element contains the part after the specified string.

Syntax

The syntax of the rpartition function in Python is:-

string.rpartition(sep)

Parameter

String rpartition function accept one parameter.

  • sep:- Required. The string search for.

Return Value

rpartition function in python returns tuple.

Python string rpartition examples:

Here we will take some example to understand Python string rpartition method.

Example:

a = 'Python is a popular programming language.Python can be used in various software domains.'
result = a.rpartition('Python')
print(result)

Output

('Python is a popular programming language.', 'Python', ' can be used in various software domains.')

Note:- If the specified value not found, rpartition() method returns a tuple contains:- empty string, empty string, whole string. As you can see in below example.

Example

a = 'Python is popular programming language.Python can be used in various software domains.'
result = a.rpartition('nothing')
print(result)

Output

('', '', 'Python is popular programming language.Python can be used in various software domains.')

Conclusion

In this tutorial, you have learned the Python string rpartition method to search for the last occurrence of the specified string. Python string rpartition() method almost same as partition() method.

The difference between these two methods are rpartition method search for the last occurrence but the partition method search for the first occurrence.

I hope this tutorial will help you. if you like this article, please share it with your friends who want to learn Python programming.

For More Information:- Click Here

Previous Article
Next Article

Was this post helpful?

Related Posts

Leave a Reply