Introduction Python String rindex() Method with Example

Introduction Python String rindex() Method with Example

The rindex() method returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an exception.

Python rindex() is an inbuilt string handling method that returns the highest index of the substring that is passed in the rindex() function as an argument. If that substring is not present in the main string, then the function throws an exception. There are three arguments passed, first is the substring, starting and ending indexes of the main string in which we want to search. To give starting and ending indexes is optional, and if we don’t give it, 0 and length of string -1 is taken as default.

Python String rindex()

Python string function rindex() returns the last index where the substring str is found or raises the exception if no such index exists, optionally restricting the search to string[beg: end].

Syntax

str.rindex(sub[, start[, end]] )

rindex() Parameters

The rindex() method takes three parameters:

  • sub - substring to be searched in the str string.
  • start and end(optional) - substring is searched within str[start:end]

Return Value from rindex()

  • If substring exists inside the string, it returns the highest index in the string where substring is found.

  • If substring doesn't exist inside the string, it raises a ValueError exception.

The rindex() method is similar to rfind() method for strings.

The only difference is that rfind() returns -1 if the substring is not found, whereas rindex() throws an exception.

Example programs on rindex() method in python

Example 1: rindex() With No start and end Argument

quote = 'Let it be, let it be, let it be'

result = quote.rindex('let it')
print("Substring 'let it':", result)
  
result = quote.rindex('small')
print("Substring 'small ':", result)

When you run the program, the output will be:

Substring 'let it': 22
Traceback (most recent call last):
  File "...", line 6, in <module>
    result = quote.rindex('small')
ValueError: substring not found

Note: Index in Python starts from 0 and not 1.

Example 2: rindex() With start and end Arguments

quote = 'Do small things with great love'

# Substring is searched in ' small things with great love' 
print(quote.rindex('t', 2))

# Substring is searched in 'll things with'
print(quote.rindex('th', 6, 20))

# Substring is searched in 'hings with great lov'
print(quote.rindex('o small ', 10, -1))


When you run the program, the output will be:

25
18
Traceback (most recent call last):
  File "...", line 10, in <module>
    print(quote.rindex('o small ', 10, -1))
ValueError: substring not found

Finally, Python String rindex() Method Example Tutorial is over.

Learn More

Thanks for reading !

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

Description
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Description
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.

Dropbox: https://bit.ly/2AW7FYF

Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more:

Learn Python Programming

Learn Python Programming

Learn Python Programming

Description
Learn Python Programming

Learn Python Programming and increase your python programming skills with Coder Kovid.

Python is the highest growing programming language in this era. You can use Python to do everything like, web development, software development, cognitive development, machine learning, artificial intelligence, etc. You should learn python programming and increase your skills of programming.

In this course of learn python programming you don't need any prior programming knowledge. Every beginner can start with.

Basic knowledge
No prior knowledge needed to learn this course
What will you learn
Write Basic Syntax of Python Programming
Create Basic Real World Application
Program in a fluent manner
Get Familiar in Programming Environment