Change language

Python String | RFIND ()

| | |

Syntax :

 str.rfind (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 indices are supplied, 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 substring index, if found in the given string; if not found, -1 is returned.

Exception:
ValueError: This error occurs when the argument string is not found in the target string.

CODE # 1

# Python program to demonstrate how rfind () works
# in the whole line

word = ’geeks for geeks’

  
# Returns the most high substring index

result = word. rfind ( ’geeks’ )

print ( "Substring’ geeks’ found at index: " , result)

 

result = word.rfind ( ’for’ )

print ( " Substring ’for’ found at index:" , result)

  

word = ’CatBatSatMatGate’

 
# Returns the highest substring index

result = word.rfind ( ’ate ’ )

print ( "Substring’ ate’ fo und at index: " , result)

Output:

 Substring ’geeks’ found at index: 10 Substring’ for’ found at index: 6 Substring ’ate’ found at index: 13 

CODE # 2

# Python program to demonstrate how rfind () works
# in a substring

word = ’ geeks for geeks’

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

print (word.rfind ( ’ge’ , 2 ))

 < br /> # The substring is searched for in & # 39; eeks for geeks & # 39;

print (word.rfind ( ’geeks’ , 2 ))

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

print ( word.rfind ( ’geeks’ , 2 ))

 
# The substring is searched for in & # 39; s for g & # 39;

print (word.rfind ( ’for’ , 4 , 11 ))

Output:

 10 10 -1 6 

Practical Application:
Useful when validating strings. Check if the given substring is present in any string or not.

# Python program to demonstrate how rfind () works
# search string

word = ’CatBatSatMatGate’

  

if (word.rfind ( ’Ate’ )! = - 1 ):

print ( "Contains given substring" )

else :

print ( "Doesn’t contains given substring" )

Output:

 Doesn’t contains given substring 

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

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method