Python string find() examples | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python string find() examples

I'm looking for examples but am having no luck. Is anyone aware of any examples on the internet? I'd like to know what it returns when it can't find something, and how to specify from start to end, which I assume will be 0, -1. >>> x = "Hello World" >>> x.find('World') 6 >>> x.find('Aloha'); -1 Could someone please assist me? But I'm not sure about it.

17th Oct 2022, 11:53 AM
sarthak jain
sarthak jain - avatar
1 Answer
+ 6
# Hi, take a look at this (run it on Playground): # s index goes from 0 to 9 (a to g). s = 'abacgaefag' print(f"{s = }") print(f"{s.find('a') = }") print(f"{s.find('a', 1, 4) = }") print(f"{s.find('a', 6) = }") print(f"{s.find('a', 3, 9) = }") # str.find() will return the start index for the string you are searching for, if it finds it. # You can restrict your searching range as shown above. # If str.find() doesn’t find what you are looking for, it will return -1. # Read more in help. print('\n') help(str.find)
17th Oct 2022, 12:10 PM
Per Bratthammar
Per Bratthammar - avatar