×

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

Pillow Python introduction and setup

Introduction Advanced image processing implies handling the picture carefully with the assistance of a PC. Utilizing picture handling we can perform activities like upgrading the picture, obscuring the picture, separating text...

4 minutes read.

Python Program for Tower of Hanoi

In this tutorial, we will examine and learn about the Python coding used to create the video game Tower of Hanoi. Before seeing the game's step-by-step implementation in Python, we...

3 minutes read.

Global variables in python

In Python, considering the scope, variables are categorized into global and local variables. In this article, we will discuss these two types along with examples. Any variable holds a value in...

4 minutes read.

Defaultdict in Python

In this tutorial, we will study What is defaultdict in Python We will understand it with the aid of certain examples. Before this, we will have a look at dictionaries...

3 minutes read.

Python Web Development projects

What is Python? Python is currently one of the most widely used computer programming languages. This isn't just an exaggeration; Python is the second-most famous programming language on the software development...

3 minutes read.

Python hex() function

Python hex() function The hex() function in Python  converts the specified number into a hexadecimal value. Syntax hex(x) Parameter x: The parameter represents an Integer value. Return This function Returns hexadecimal string. Example 1 # Python Program explaining # the hex() function print("The...

1 minute read.

Iterate a Dictionary in Python

In this tutorial, we will learn how to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to store data values,...

3 minutes read.

GUI to extract lyrics from a song Using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

4 minutes read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

Python Console

Console in Python is referred as the command line Interpreter- (CLI) and also knows as Shell and it functions as taking input from the human user and interpreting it through...

6 minutes read.

Python Variables, Constants and Literals

Python is today's most valuable, easy-to-implement and sought-out programming language. Nowadays, developers want to focus on implementing the program rather than spending time with complex programs. So, for this reason,...

17 minutes read.

Python Pass Statement

The pass statement is a null statement. The difference between pass and comment is that comment is ignored by the interpreter, whereas pass is not. The pass statement is typically used...

3 minutes read.

Is Python Case Sensitive

Case sensitivity is the mode of dealing with the written alphabet. The cases of the alphabet are examined and based on these words are being treated. The uppercase and lowercase...

3 minutes read.

Python Project Ideas

One of the most widely used programming languages today is Python. This pattern appears set to continue through 2023 and beyond. Therefore, working on some current Python project ideas is the...

10 minutes read.

Python Multiprocessing Processor

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....

4 minutes read.

Python Dictionary fromkeys() method

Python Dictionary fromkeys() method The dictionary.fromkeys() method in Python creates a new dictionary from the given sequence of elements and returns a dictionary with the specified keys and values. Syntax dictionary.fromkeys(sequence[, value]) Parameter sequence- This...

1 minute read.

Best Way to Learn Python for Free

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. Learning Python is a step towards coding. Python gets...

5 minutes read.

Python Built-in Functions

Python Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. The Python built- in functions are as follow: Methods Explaining abs() The abs() function returns...

6 minutes read.

Convert Float to Int in Python using Pandas

Introduction To play with huge amounts of data, in python we require a tool. The tool which is available in Python is pandas. A panda is an open-source library. It is...

4 minutes read.

Python String expandtabs() method

Python String expandtabs() method The string.expandtabs() method in Python returns a copy of the string where all tab characters are expanded using spaces. Syntax String.expandtabs([tabsize]) Parameter tabsize: This parameter specifies the number of characters to...

1 minute read.