
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Python - OS Path Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Classes & Objects
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Advanced Concepts
- Python - Abstract Base Classes
- Python - Custom Exceptions
- Python - Higher Order Functions
- Python - Object Internals
- Python - Memory Management
- Python - Metaclasses
- Python - Metaprogramming with Metaclasses
- Python - Mocking and Stubbing
- Python - Monkey Patching
- Python - Signal Handling
- Python - Type Hints
- Python - Automation Tutorial
- Python - Humanize Package
- Python - Context Managers
- Python - Coroutines
- Python - Descriptors
- Python - Diagnosing and Fixing Memory Leaks
- Python - Immutable Data Structures
- Python Useful Resources
- Python - Questions & Answers
- Python - Interview Questions & Answers
- Python - Online Quiz
- Python - Quick Guide
- Python - Reference
- Python - Cheatsheet
- Python - Projects
- Python - Useful Resources
- Python - Discussion
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
Python String rindex() Method
Python string method rindex() searches for the last index where the given substring str is found in an original string. This method raises an exception if no such index exists, optionally restricting the search to string length, i.e. from the first index to the last. This method is almost similar to the rfind() method, but the difference is that the rfind() method does not raise an exception (Value Error) and returns -1 if the substring is not found.
However, if there are multiple occurrences of the same substring, the methods finds the last index of the last occurence.
Syntax
Following is the syntax for Python String rindex() method −
str.rindex(str, beg=0 end=len(string))
Parameters
str − This specifies the string to be searched.
beg − This is an optional parameter, and represents the starting index. Its default is 0
len − This is an optional parameter, and represents the ending index, Its default is equal to the length of the string.
Return Value
This method returns last index if found otherwise raises an exception if str is not found.
Example
When two strings are taken as input and, both rindex() and index() methods are called; the index of the first occurrence of substring is returned by the index() method while the index of the last occurrence is returned by the rindex() method.
The following example shows the usage of Python String rindex() method.
str1 = "this is string example....wow!!!"; str2 = "is"; print str1.rindex(str2) print str1.index(str2)
When we run above program, it produces following result −
5 2
Example
When we input a string and pass another string as a substring parameter, the method returns the last index of the substring.
In this example, we create a string and invoke the rindex() method by passing a substring as an argument.
# inputting a string string = 'Tutorialspoint is a great place to learn Python. Tutorialspoint is an ian company' # finding the 'Tutorialspoint' using rindex print(string.rindex('Tutorialspoint')) # finding the 'is' using rfind print(string.rindex('is'))
The output produced by the program above is given as follows −
49 64
Example
If we pass three parameters (including optional) to the method, the last index of the substring within the given index limit is detected.
In this example, the method is called on an input string and it takes three parameters: a substring to be located, start index and the end index.
# inputting a string string = 'Tutorialspoint is a great place to learn Python. Tutorialspoint is an ian company' # finding the 'Tutorialspoint' using rindex print(string.rindex('Tutorialspoint', 0, 45)) # finding the 'is' using rfind print(string.rindex('is', 0, 45))
Let us run the program above, and obtain the output as follows −
0 15
Example
If the substring given as the parameter to the method does not exist in the string, the method raises a ValueError.
In the given program, a string is created and the method is invoked on it. The substring passed as a argument to the method is not a part of the input string, the method raises an exception.
# inputting a string string = 'Tutorialspoint is a great place to learn Python. Tutorialspoint is an ian company' # finding the 'Tutorialspoint' using rindex print(string.rindex('tutorialspoint'))
When we try to run the program, the ValueError is raised instead of printing the output −
--------------------------------------------------------------------------- ValueError Traceback (most recent call last)in 3 4 # finding the 'Tutorialspoint' using rfind ----> 5 print(string.rindex('tutorialspoint')) ValueError: substring not found
Example
Even if the substring is present in the string but not within the limit provided by the optional parameters passed to this method, a ValueError is raised.
# inputting a string string = 'Tutorialspoint is a great place to learn Python. Tutorialspoint is an ian company' # finding the 'Tutorialspoint' using rindex print(string.rindex('Python', 0, 15))
When we run above program, it produces following result −
Traceback (most recent call last): File "main.py", line 4, inprint(string.rindex('Python', 0, 15)) ValueError: substring not found