Change language

Python String | find()

| |

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

Returns:

returns the lowest index of the substring if it is found in given string. If it’s not found then it returns -1.

CODE 1

 

word = ’geeks for geeks’

  
# returns the first occurrence of Substring

result = word.find ( ’ geeks’ )

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

  

result = word.find ( ’for ’ )

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

  
# How to use find ()

if (word.find ( ’pawan’ )! = - 1 ):

  print ( "Contains given substring" )

else :

print ( "Doesn’t contains given substring" )

Output:

 Substring ’geeks’ found at index: 0 Substring’ for ’found at index: 6 Doesn’t contains given substring 

Code 2

word = ’geeks for geeks’

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

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

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

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

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

print (word.find ( ’g’ , 4 , 10 )) 

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

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

Output:

 10 -1 -1 6 

Python String | find() find: Questions

Python String | find() Python functions: Questions

Shop

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

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

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