Python String rfind()

The syntax of rfind() is:

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

rfind() Parameters

rfind() method takes a maximum of three parameters:

  • sub - It's the substring to be searched in the str string.
  • start and end (optional) - substring is searched within str[start:end]

Return Value from rfind()

rfind() method returns an integer value.

  • If substring exists inside the string, it returns the highest index where substring is found.
  • If substring doesn't exist inside the string, it returns -1.
How find() and rfind() works in Python?
Return Value from rfind()

Example 1: rfind() With No start and end Argument

Output

Substring 'let it': 22
Substring 'small ': -1
Highest index where 'be,' occurs: 18

Example 2: rfind() With start and end Arguments

Output

-1
25
-1
18