×

Python String rindex() method

Python String rindex() method

The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an exception.

Syntax

string.rindex(sub [,start [,end]])

Parameter

sub: This parameter represents the value to search for.

start( Optional): This parameter signifies the substring from where the search is to start and its

default value is 0.

end: This parameter signifies the substring from where to end the search and its default value is the end of the string.

Return

This method returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an exception. 

Example 1

 # Python program explaining
 # the string.rindex() method
 # initializing the String
 string ="She sells sea shells by the sea shore"
 # finding the highest index of substring 'sea' in the string 
 index = string.rindex("sea")
 # printing the index
 print("The index of 'sea':",index) 

Output

The index of 'sea': 28

Example 2

 # Python program explaining
 # the string.rindex() method
 # initializing the String
 string ="She sells seashells by the sea shore"
 # finding the highest index of substring 'by' in the string 
 result = string.rindex('by')
 if  (result != -1):
   print("Highest index where 'by' occurs:", result)
 else:
   print("Doesn't contain substring") 

Output

Highest index where 'by' occurs: 20

Example 3

 # Python program explaining
 # the string.rindex() method
 # initializing the String
 string ="She sells seashells by the sea shore"
 # Substring 'se' is searched in the specified string
 print(string.rindex('se', 10))
 # Substring 'h' searched 
 print(string.rindex('h', 2))
 # Substring 's' is searched
 print(string.rindex('s', 10, -1))
 # Substring 'hi' searched 
 # will raise a ValueError 
 print(string.rindex('hi')) 

Output

 27
 32
 31
 Traceback (most recent call last):
   File "main.py", line 17, in <module>
     print(string.rindex('hi'))
 ValueError: substring not found 

Related Topics

Google Chrome API in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Flutter Python

The flutter is a frame work available in the python programming language, to create web applications, mobile apps. The flutter is generally used for the development of the backend of...

3 minutes read.

How to Install Python In Windows

How To Install Python In Windows? Python is a language that every aspirant developer looks forward to. One thing we must keep in our mind is how to begin our hands-on...

3 minutes read.

Python Slice from Last Occurrence of K

Introduction We already know that in Python, cutting produces a sub-string out of a string. The variables start, stop, and step are used to set the slicing range. When dealing on...

3 minutes read.

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python. Face recognition is of great utility in real-world scenarios. It is an extended step...

3 minutes read.

Append key Value to Dictionary in Python

The Python dictionary is one of the built-in data types. Elements of dictionaries are key-value pairs. In Python, there are numerous ways to add dictionaries. Let's examine some of the...

9 minutes read.

Python Comments

In this tutorial, we will discuss the importance of comments in the Python code and how various types of comments can be inserted in the code. Comments are used to describe...

3 minutes read.

Python SciPy Library

Introduction SciPy, a logical library for Python is an open-source, BSD-authorized library for arithmetic, science, and design. The SciPy library relies upon NumPy, which gives advantageous and quick N-dimensional exhibit control....

6 minutes read.

Writing to Excel using Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.

Python Errors and exceptions

Exceptions and errors are the obstacles a programmer constantly faces while writing a program. Firstly, we need to understand what are errors and exceptions and the difference between these two...

6 minutes read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.

Google Python Class

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Text detection using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Python Write Excel File

Python Write Excel File The Python xlwt module is used to write an excel file and perform multiple operations on it. It can be used to write text, numbers, and formulas for multiple...

3 minutes read.

Python map() function

Python map() function The map() function in Python returns an iterator that applies a function to every item of iterable, yielding the results. Syntax map(function, iterable, ...) Parameter function: It is a required parameter that represents the function to...

1 minute read.

Spyder (32-bit) - Free download

Spyder is a free and open-source scientific environment created by and for Python engineers, scientists, and data analysts. It is a robust scientific environment created in Python by and for...

3 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Get index of element in array in python

Index : Index is a number where the element is located in the given list. In other words it is the position of the element in the list. Index can also...

4 minutes read.

Python hash() function

Python hash() function The hash() function in Python returns the hash value of the object (if it has one).  Syntax hash(object) Parameter obj : This parameter represents the object which we need to convert into hash. Return This...

1 minute read.

Python Program to Find the gcd of Two Numbers

Introduction Greatest Common Divisor is the full form of gcd. The greatest common divisor, or GCD, of two numbers, is a value that can exactly divide the two digits and is...

5 minutes read.