×

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

How to Install Scikit-Learn

Sklearn or Scikit-learn is a python library used for machine learning. It contains many features like classification, regression, clustering, and Dimensionality reduction algorithms. Sklearn is used to build machine learning...

3 minutes read.

Python Set union() method

Python Set union() method The set.union() method in Python returns a set that contains all items from the original set and all items from the specified sets. Syntax set.union(set1, set2...) Parameter set1- This parameter represents the first set...

2 minutes read.

Python String title() method

Python String title() method The string.title() method in Python returns a string where the first character in every word is upper case. If the word contains a number or a symbol,...

1 minute read.

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

One Liner If-Else Statements in Python

Before learning one liner if else Python, let us first know what Python is. Python is a broadly helpful, object-oriented and dependable language having a large number of utilizations from...

4 minutes read.

NOT IN operator in Python

This page contains all the information about “not in” operator in python. You can also know everything about what type of operator is “not in” in python language and where...

7 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

*Args and **Kwargs in Python

Let’s know about ** (dual star / asterisk) and * (star / asterisk) are operators in Python. These stars are used in Python for parameters. Args and kwargs are special...

3 minutes read.

Math Module in Python

In this article, you are going to learn everything in detail about the “ math “ module in Python. We can normally work with general operations using python without importing or...

16 minutes read.

Working with CSV files in 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...

7 minutes read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

Function annotations() in Python

What is Function Annotations? Function annotations provide a way related to different parts of a function at compile time with arbitrary Python expressions. It doesn’t have life in Python runtime execution....

3 minutes read.

Decision Tree Classification in Python

In this article, we will learn the implementation of the decision tree in Sklearn, which is nothing but the Scikit Learn library of python. First, we should learn what classification...

12 minutes read.

Python exe() function

Python exe() function The exec() function in Python executes the specified Python code and accepts large blocks of code. It supports dynamic execution of Python code where the object must be either a string...

1 minute read.

Convert String to Binary in Python

String to binary The strings can be defined as the array of Unicode code characters. Binary Binary is defined as the number system which consists two symbols 0 and 101. It is base-2...

2 minutes read.

Python Support

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.

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.

Python Lists vs Tuples

The difference between lists and tuples is one of the most frequently asked questions in an interview related to python language. Lists and Tuples are two of Python’s built-in data...

4 minutes read.

How To Print Colored Text in Python

Changing the colour of certain parts of a string when printing the output of a Python programme to the terminal may make it easier to read. We can approach this...

3 minutes read.