Python String rfind() Method Examples

In the previous article, we have discussed Python Program for partition() Function
rfind() Function in Python:

The rfind() method gives the position of the last occurrence of the specified value.

If the value cannot be found, the rfind() method returns -1.

The rfind() method is nearly identical to the rindex() method.

Syntax:

string.rfind(value, start, end)

Parameters

value: This is required. The value to look for

start: This is optional. Where to begin your search. The default value is 0.

end: This is optional. Where to put an end to the search. The default value is to the end of the string.

Examples:

Example1:

Input:

Given string =  "welcome to python learning platform"
Given value = "p"

Output:

The result after applying rfind() function on the given string =  27

Example2:

Input:

Given string = "welcome to python learning platform"
Given value = "o"
Given start index = 1
Given end index = 8

Output:

The result after applying rfind() function on the given string =  4

String rfind() Method Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Apply rfind() function to the given string for the given value that gives the last occurrence of the specified value.
  • Store it in another variable.
  • Print the result after applying rfind() function on the given string for the given value.
  • Similarly, do the same for the other values and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "welcome to python learning platform"
# Give the value as static input and store it in another variable.
gvn_valu = "p"
# Apply rfind() function to the given string for the given value that gives the last
# occurrence of the specified value.
# Store it in another variable.
rslt = gvn_str.rfind(gvn_valu)
# Print the result after applying rfind() function on the given string
# for the given value.
print("The result after applying rfind() function on the given string = ", rslt)
# Similarly, do the same for the other values and print the result.
rslt2 = gvn_str.rfind('o', 1, 8)
rslt3 = gvn_str.rfind('b')
print("The result after applying rfind() function on the given string = ", rslt2)
print("The result after applying rfind() function on the given string = ", rslt3)

Output:

The result after applying rfind() function on the given string =  27
The result after applying rfind() function on the given string =  4
The result after applying rfind() function on the given string =  -1

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Apply rfind() function to the given string for the given value that gives the last occurrence of the specified value.
  • Store it in another variable.
  • Print the result after applying rfind() function on the given string for the given value.
  • Similarly, do the same for the other value by giving the start and end positions as user input using the int(input()) function and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Give the value as user input using the input() function and 
# store it in another variable.
gvn_valu = input("Enter some random value = ")
# Apply rfind() function to the given string for the given value that gives the last
# occurrence of the specified value.
# Store it in another variable.
rslt = gvn_str.rfind(gvn_valu)
# Print the result after applying rfind() function on the given string
# for the given value.
print("The result after applying rfind() function on the given string = ", rslt)
# Similarly, do the same for the other value by giving the start and end positions
# as user input using the int(input()) function and print the result.
gvn_valu2 = input("Enter some random value = ")
gvn_strtindx = int(input("Enter some random number = "))
gvn_endindx = int(input("Enter some random number = "))
rslt2 = gvn_str.rfind(gvn_valu2, gvn_strtindx, gvn_endindx)
print("The result after applying rfind() function on the given string = ", rslt2)

Output:

Enter some random string = hello this is btechgeeks
Enter some random value = e
The result after applying rfind() function on the given string = 21
Enter some random value = t
Enter some random number = 1
Enter some random number = 20
The result after applying rfind() function on the given string = 15

Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.