| by Arround The Web | No comments

Python String rindex() Method

Like other computer programming languages, Python is utilized for making websites, applications, automating tasks, and also conduct data analysis. Developers can perform these operations by using several in-built methods for working with several data types. For instance, operating on any type of string with the help of the “rindex()” method.

In this guide, we will briefly demonstrate the “string.rindex()” method in Python.

What is the “string.rinder()” Method in Python?

To find the highest index of the specified substring inside the input string in Python, the “string.rindex()” method can be used. If the provided substring is found, as an output it will return its index otherwise it will throw ValueError.

Syntax
The general syntax of the above-specified method is:

str.rindex(sub, start, end)

Here, the “str.rinder()” method takes three arguments:

  • sub” parameter is mandatory that shows the values to search for in the specified string.
  • start” argument indicates where to start the search, and its default value is set to “0”. It is an optional parameter.
  • end” parameter is also an optional parameter and is used to specify the point where the search ends.

Example 1: How to Use “string.rindex()” Method with Start or End Index in Python?

If users want to get the index from a particular range, the “string.rindex()” method can be used along with the required parameters. To do so, first, we declared the input string named “input_str”:

input_str = "Welcome to Linuxhint World!!"

Then, call the “string.rindex()” method and pass it the substring “to”, then, “8” as a starting index and “11” as an ending index. After that, store into the “resultant_str” variable:

resultant_str = input_str.rindex("to", 8, 11)

Lastly, apply the “print()” statement and pass it to the variable that holds the resultant value to display on the screen:

print('The highest index of \'to\' is = ', resultant_str)

According to the below-given output, the highest index of the specified string is “8”:

Example 2: How to Use “string.rindex()” Method Without Start or End Index in Python?

Let’s take another example to get the highest index of the provided substring without specifying the starting and ending index. Here, we used the previously discussed string as the input string. Then, call the “string.rindex()” method and pass it only to the desired substring as a parameter. Then, save results in the “resultant_str” variable:

resultant_str = input_str.rindex("to")

Lastly, apply the “print()” method to get the resultant value passing the variable as an argument:

print('The highest index of \'to\' is = ', resultant_str)

Output

That’s all about the “string.rindex()” method in Python.

Conclusion

In Python, the “string.rindex()” method is used to get the highest index of the specified substring inside the input string. If the provided substring is retrieved, then it will return its index as an output otherwise it will throw ValueError. This guide illustrated about the “string.rindex()” method in Python.

Share Button

Source: linuxhint.com

Leave a Reply