Python String rfind() Method



The Python String rfind() method searches for the starting index of a last occurrence of a specified substring is found in an original string. Unlike the rindex() method, which raises an exception if the substring is not present, this method returns -1.

This method performs the search in two scenarios:

  • It searches the entire string from the 0th index to the end index, or,
  • It searches a part of the string, from the ith index to jth index, where i < j.

Syntax

Following is the syntax for Python String rfind() method −

str.rfind(sub[, start[, end])

Parameters

  • str − This specifies the string to be searched.

  • start − This is the starting index, by default its 0.

  • end − This is the ending index, by default its equal to the length of the string.

Return Value

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

Example

When we pass a substring of the original string as a parameter to the method, the return value will be the index of the substring in the string.

The following example shows the usage of Python String rfind() method. Here, we create a string, say "This is a string", and pass a substring of this string, say "is", as an argument to the method. The method finds the index of this substring in the input string.

str1 = "This is a string";
str2 = "is";

print("The index of substring:")
print(str1.rfind(str2))

When we run above program, it produces following result −

The index of substring:
5 

Example

When we pass a substring that is not a part of the original string as a parameter to the method, the return value will be -1.

In this example, we will create a string: "This is a string" as input and pass another string "Hello" as an argument to the method. Since the string does not contain the argument, the method returns -1.

str1 = "This is a string";

print("The index of substring:")
print(str1.rfind('Hello"'))

Let us run the program above, the output is obtained as follows −

The index of substring:
-1

Example

We pass optional parameters to the method determining the range of the search, the method will return the occurrence of the string within that range.

In the given program, we take a string as an input. We pass three parameters to the method called on this input string: a substring, a beginning index and an ending index. The method searches for the specified substring within the limit and returns the index.

str1 = "This is a string";

print("The index of substring:")
print(str1.rfind('is', 2, 5))

The output for the program above is given as follows −

The index of substring:
2

Example

We pass optional parameters to the method determining the range of the search and the substring is not present in the given range, the method will return -1.

In the given program, we pass three parameters to the method called on an input string created: a substring, a beginning index and an ending index. The method searches for the specified substring within the limit and returns the index. The return value will be -1 if the substring is not present in the limit specified.

str1 = "This is a string";

print("The index of substring:")
print(str1.rfind('st', 0, 10))

Let us run the program to display the output as follows −

The index of substring:
-1
python_strings.htm
Advertisements