Site icon i2tutorials

rfind() method in Python String Module

rfind() method in Python String Module


rfind method in string module searches the specified word in the string and gives its index position where was it found in the string. If the word is repeated is more than once then it gives the index position where it was founded last. If the specified is not found it returns the output as -1.


Syntax:

str.rfind()


Parameters:

There are 3 parameters in the rfind method.

  1. Sub: this is the substring which needs to found
  2. Start: the start position from where the substring needs to be found. This is an optional parameter.
  3. End: this is the position where the substring needs to be stopped searching. This is an optional parameter.


Example-1:

rfind method using sub parameter

text = "give me the pencil, give me the pen, give me the scale."
x = text.rfind("give")
print(x)


Output:


Example 2:

rfind method using Sub, start and end parameters.

text = "give me the pencil, give me the pen, give me the scale."
x = text.rfind("give", 0, 39)
print(x)


Output:

Exit mobile version