YouTip LogoYouTip

Python3 String Rfind

Python3 rfind() Method

Python3 rfind() Method

Description

The Python rfind() method returns the highest index (last occurrence) of a substring within a string. If the substring is not found, it returns -1.

Syntax

The syntax for the rfind() method is:

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

Parameters

  • str -- The substring to search for.
  • beg -- The starting index for the search. Default is 0.
  • end -- The ending index for the search. Default is the length of the string.

Return Value

Returns the highest index (last occurrence) of the substring within the string. If the substring is not found, it returns -1.

Example

The following example demonstrates the usage of the rfind() function:

Example

#!/usr/bin/python3

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))

The output of the above example is as follows:

5
5
-1
2
2
-1
← Git PushPython3 String Maketrans β†’