Why s.find('') and s.find('s') returns 0 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why s.find('') and s.find('s') returns 0 ?

s = 'star' print(s.find(''),s.find('s')) in the above code it prints 0,0 as output. why?

20th Jul 2017, 10:13 AM
Harish M
Harish M - avatar
1 Answer
+ 5
In python indexing starts from 0, so indexes of characters in string 'star' will be 0123. s.find('s') return the index of 's' which is 0 s.find('') return 0 not because nothing has been found but because passing '' as a parameter just return the string itself (starting at 0) if the string you are searching for is not in the searched string then find will return -1 s.find('1') = -1
20th Jul 2017, 12:14 PM
Maya
Maya - avatar