- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find index of last occurrence of a substring in a string in Python?
A string is a collection of characters that can represent a single word or a whole sentence. We can declare a string or any other datatype in python without the datatype specifier. Therefore, it is easy to use strings in Python compared to Java.
A string is an object of the String class, which contains multiple methods to manipulate and access strings.
In this article, we are going to focus on how to find index of last occurrence of a substring in a string in Python.
Using the rindex() method
One way to find the index of last occurrence of a substring is using the rindex() method from the inbuilt python String class. It accepts a string representing the substring to be found as a parameter, and returns the last index of the given string in the current one.
The main drawback of this function is that it throws an exception if there is no substring present in the string.
To overcome the drawback of rindex(), there is another function named rfind(). Its functionality is similar to that of rindex(), but this method does not return an exception if the given substring is not present in the string, instead, it returns ‘ 1’ indicating that the given substring is not present.
Both rindex() and rfind() have optional parameters where you can mention the start index from where you would want to start and the end index
Example 1
In the example given below, we are using the method rindex() to find out the last occurrence of the character ‘t’ in the given string.
str1 = "Welcome to tutorialspoint" index = str1.rindex('t') print("The last index of t in the given string is",index)
Output
The output of the above given example is,
('The last index of t in the given string is', 24)
Example 2
In the example given below, we are trying to find out the last index of the character ‘d’ in the given string using rindex() method, but the character ‘d’ is not present in the string.
str1 = "Welcome to tutorialspoint" index = str1.rindex('d') print("The last index of t in the given string is",index)
Output
The output of the above given example is,
Traceback (most recent call last): File "main.py", line 3, inindex = str1.rindex('d') ValueError: substring not found
Example 3
In the example given below, we are using the method rfind() to find out the last occurrence of the character ‘t’ in the given string.
str1 = "Welcome to tutorialspoint" index = str1.rfind('t') print("The last index of t in the given string is",index)
Output
The output of the above given example is,
('The last index of t in the given string is', 24)
Example 4
In the example given below, we are trying to find out the last index of the character ‘d’ in the given string using the rfind() method, but the character ‘d’ is not present in the string.
str1 = "Welcome to tutorialspoint" index = str1.rfind('d') print("The last index of t in the given string is",index)
Output
The output of the above example is,
('The last index of t in the given string is', -1)
- Related Articles
- How to find the nth occurrence of substring in a string in Python?
- Get the last occurrence of a substring within a string in Arduino
- Get the index of last substring in a given string in MySQL?
- How to get the last index of an occurrence of the specified value in a string in JavaScript?
- How to replace the last occurrence of an expression in a string in Python?
- How to find the index of the last occurrence of repeated values in a vector in R?
- How to find the last index value of a string in Golang?
- Get the substring before the last occurrence of a separator in Java
- Find last index of a character in a string in C++
- How is it possible in MySQL to find the location of the first occurrence of a substring in a string?
- Get the first occurrence of a substring within a string in Arduino
- How to check index within a string of a specified substring in JSP?
- Finding the last occurrence of a character in a String in Java
- MySQL String Last Index Of in a URL?
- Last occurrence of some element in a list in Python
