Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python rindex

by suresh

The Python rindex method is used to return the index position of the last occurrence of a specified string. Python rindex function return ValueError if the specified string not found. In this section, we discuss how to write rindex Function in Python Programming with example.

TIP: The index position in the rindex Function start from 0, Not 1.

Python rindex Syntax

The syntax of the Python rindex function is

String_Value.RIndex(Substring, Starting_Position, Ending_Position)
  • String_Value: Please select the valid String literal.
  • Substring: Please specify the string you want to search for.
  • Starting_Position: This is an optional parameter. If you want to specify the starting point (starting index position), then Please specify the index value here. If you omit this parameter, the rindex Function considers Zero as a starting position.
  • Ending_Position: This is an optional parameter. If you want to specify the endpoint (Ending index position), then Please specify the index value here. If you omit this parameter, the rindex Function considers the highest index number.

Python rindex method Example

The following set of examples help you understand the rindex Function.

# Python RIndex Method Example

Str1 = 'We are abc working at abc company';

Str2 = Str1.rindex('abc')
print('First Output of a RIndex() method is = ', Str2)

# Performing RIndex() function directly
Str3 = 'Find Tutorial at Tutorial Gateway'.rindex('Tutorial')
print('Second Output of a RIndex() method is = ', Str3)

# Using First Index while finding the String
Str4 = Str1.rindex('abc', 12)
print('Third Output of a RIndex() method is = ', Str4)

# Using First & Second Index while finding the String
Str5 = Str1.rindex('abc', 2, 21)
print('Fourth Output of a RIndex() method is = ', Str5)

# Searching for Not existing Item
Str6 = Str1.rindex('Tutorial')
print('Fifth Output of a RIndex() method is = ', Str6)
Python RIndex

First, it finds the last occurrence of a substring ‘abc’ inside the Str1 using rindex function and prints the output

Str2 = Str1.rindex('abc')
print('First Output of an Index() method is = ', Str2)

The rindex function allows us to use the Starting index position. By specifying the starting index position, we can increase performance.

Str4 = Str1.rindex('abc', 12)
print('Third Output of a RIndex() method is = ', Str4)

The Python rindex function allows us to use Starting and ending indices. By specifying the starting and ending index positions, we can increase performance. The following statement starts looking for ‘abc’ from index position 2 and ends at index position 21. As we all know, the second abc is in position 22. So, Python returns the index position of the first abc.

Str5 = Str1.rindex('abc', 2, 21)
print('Fourth Output of a RIndex() method is = ', Str5)

The following String Method statement is returning Value Error because, rindex function start looking for substring ‘Tutorial’ inside the Str1, which does not exist.

Str6 = Str1.rindex('Tutorial')
print('Fifth Output of a RIndex() method is = ', Str6)

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy