logo

Python String Rfind() Method


Show

Description

Python String rfind() Method is another inbuilt function in Python Programming that is responsible to return the copy of all the strings where each occurrence of the substring has been necessarily replaced for another substring. The replace() method in Python String thus replaces the specific phrase with some other specified phrase. The Python string replace() method also does not change the original string

Syntax

Following is the syntax for rfind() method:

obj.rfind(str, beg=0 end=len(string))

Parameters

  • str - This specifies the string to be searched.
  • beg - This is the starting index, by default, it's 0.
  • end - This is the ending index, by default it's equal to the length of the string.

Return Value

This method returns the last index if found and -1 otherwise.

Example

The example below shows the usage of rfind() method.

#!/usr/bin/python

str1 = "this is really a string example....wow!!!";
str2 = "is";

print str1.rfind(str2)
print str1.rfind(str2, 0, 10)
print str1.rfind(str2, 10, 0)

print str1.find(str2)
print str1.find(str2, 0, 10)
print str1.find(str2, 10, 0)

When the above program is run, then it produces the following result:

5
5
-1
2
2
-1

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.