Change language

Rindex () string in python

| | |

Syntax :

str.find (sub, start, end)

Parameters:

sub: It’s the substring which needs to be searched in the given string.
start: Starting position where sub is needs to be checked within the string.
end: Ending position where suffix is ​​needs to be checked within the string.

NOTE. If no start and end indexes are provided, then it defaults to 0 and length-1 as start and end indices, where end indices are not included in our search.

Return:

Returns the highest index of the substring inside the string if substring is found. Otherwise it raises an exception.

Examples :

 input: text = ’geeks for geeks’ result = text.rindex (’ geeks ’) output: 10 input: text =’ geeks for geeks’ result = text.rindex (’ge’) output: 10 

Errors and exceptions:
ValueError : This error occurs when no argument string is found in the target string.

CODE 1

Output:

 Substring ’geeks’: 10 

Code 2

# Python -code to demonstrate how rindex () works

text = ’ geeks for geeks’

 

result = text.rindex ( ’geeks’ )

print ( "Substring’ geeks’: " , result)

# Python code to show the error using rindex ()

text = ’geeks for geeks’

  

result = text.rindex ( ’pawan’ )

print ( "Substring’ pawan’: " , result)

Error:

 Traceback (most recent call last): File "/ home / da dc555d90806cae90a29998ea5d6266.py ", line 6, in result = text.rindex (’ pawan’) ValueError: substring not found 

CODE 3

# Python code to demonstrate how rindex () works
# with the specified range

quote = ’geeks for geeks’

 
# The substring is searched for in & # 39; geeks for geeks & # 39;

print (quote.rindex ( ’ge’ , 2 ))

  
# The substring is searched in the range from 0 to 10

print (quote.rindex ( ’geeks’ , 0 , 10 ))

Output:

 10 0 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically