In the previous article, we have discussed Python String rfind() Method Examples
rindex() Function in Python:
The rindex() method returns the location of the last occurrence of the specified value.
If the value is not found, the rindex() method throws an exception.
The rindex() method is nearly identical to the rfind() method.
Note: The only difference is rindex() method throws an exception if the value is not found whereas rfind() returns a value -1.
Syntax:
string.rindex(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 rindex() function on the given string = 27
Example2:
Input:
Given string = "good morning btechgeeks" Given value = 'e' Given start index = 3 Given end index = 16
Output:
The result after applying rindex() function on the given string = 15
 String rindex() 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 rindex() function to the given string for the given value that returns the location of the last occurrence of the specified value.
- Store it in another variable.
- Print the result after applying rindex() function on the given string for the given value.
- Similarly, do the same for the other values by giving the start and end positions as static input 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 rindex() function to the given string for the given value that # returns the location of the last occurrence of the specified value. # Store it in another variable. rslt = gvn_str.rindex(gvn_valu) # Print the result after applying rindex() function on the given string # for the given value. print("The result after applying rindex() function on the given string = ", rslt) # Similarly, do the same for the other values and print the result. rslt2 = gvn_str.rindex('o', 1, 8) rslt3 = gvn_str.rindex('m') print("The result after applying rindex() function on the given string = ", rslt2) print("The result after applying rindex() function on the given string = ", rslt3)
Output:
The result after applying rindex() function on the given string = 27 The result after applying rindex() function on the given string = 4 The result after applying rindex() function on the given string = 34
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 rindex() function to the given string for the given value that returns the location of the last occurrence of the specified value.
- Store it in another variable.
- Print the result after applying rindex() 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 rindex() function to the given string for the given value that # returns the location of the last occurrence of the specified value. # Store it in another variable. rslt = gvn_str.rindex(gvn_valu) # Print the result after applying rindex() function on the given string # for the given value. print("The result after applying rindex() 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.rindex(gvn_valu2, gvn_strtindx, gvn_endindx) print("The result after applying rindex() function on the given string = ", rslt2)
Output:
Enter some random string = good morning btechgeeks Enter some random value = o The result after applying rindex() function on the given string = 6 Enter some random value = e Enter some random number = 3 Enter some random number = 16 The result after applying rindex() 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.