×

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found.

Syntax

string.rfind(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 in the string is the substring sub is found else it returns -1 if the substring is not found. 

Example 1

 # Python program explaining
 # the string.rfind() 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.rfind("sea")
 # printing the index
 print("The index of 'sea':",index) 

Output

The index of 'sea': 28

Example 2

 # Python program explaining
 # the string.rfind() method
 # initializing the String
 string ="She sells seashells by the sea shore"
 # finding the highest index of substring 'by' in the string 
 result = string.rfind('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.rfind() method
 # initializing the String
 string ="She sells seashells by the sea shore"
 # Substring 'se' is searched in the specified string
 print(string.rfind('se', 10))
 # Substring 'h' searched 
 print(string.rfind('h', 2))
 # Substring 's' is searched
 print(string.rfind('s', 10, -1))
 # Substring 'hi' searched 
 # will return -1 as the substring 'hi' is not present 
 print(string.rfind('hi')) 

Output

 27
 32
 31
 -1 

Related Topics

Python Call Function

In this article, you will learn about calling a function in Python. But before learning this, we should know about functions in Python.So, let’s get some overview of functions in...

6 minutes read.

Python program to print array element present at even position

Python program to print array element present at even position In this program, we'll see a Python program that prints the elements of an array that are in even positions. We...

1 minute read.

Insertion Sort using Python

Insertion sort is a type of sorting technique that is used for sorting an array with random elements. Using sorting methods, any unsorted array can be sorted into ascending or...

3 minutes read.

Python vs HTML

Python and HTML are not comparable since they are two separate categories of programming languages. Building the structures and layouts of a web page or app requires the usage of HTML,...

8 minutes read.

How to append a string in python

Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages. Ways to Append...

4 minutes read.

Executing Shell Commands in Python

This tutorial aims to make us understand what a Shell is, what is the importance of a Shell, what are Shell commands in Python, and how can we execute the...

3 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

Python abs() function

Python abs() function The abs() function returns the absolute value of a number. Syntax abs(x) Parameter x: The parameter ‘x’ can be an integer value, a floating point number or a complex number. Return This function returns...

1 minute read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

How to find square root in python

How to find Square Root of a number In Python Python makes a lot of tasks easier by using different functions, modules, and libraires. There is an inbuilt function in Python...

6 minutes read.

SKLearn Linear Module

The SK learn linear module is one such module that helps to study the relationship between the independent and dependent variables.The linear module can be implemented by using the best...

3 minutes read.

Convert XML to JSON in Python

XML conversion is very useful if we work on an API that returns data in JSON format and the source of data is in XML format. JSON A JSON file reserves the...

4 minutes read.

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

Python exe() function

Python exe() function The exec() function in Python executes the specified Python code and accepts large blocks of code. It supports dynamic execution of Python code where the object must be either a string...

1 minute read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

How to open a file in python

Opening a File in Python Python is a user-friendly programming language that makes almost every concept easy. It provides many inbuilt functions in its libraries to work with files. Using these...

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

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.

Python Hash Table

An Introduction to Hashing A technique which used to identify a particular object uniquely from a set of similar objects is known as Hashing. Some real-life examples of hashing implementations include: A...

9 minutes read.