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

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