String Methods
find()
Find the first position of some text without raising an error when it is missing.
str.find(sub, start=0, end=len(str))
str.find() returns the lowest index where substring sub is found within the slice [start, end].
If sub is not found, it returns -1 instead of raising an exception.
Parameters
| Name | Description |
|---|---|
| sub | The text to look for. |
| start | Optional starting position. |
| end | Optional position where the search stops. |
Returns
The first index, or -1 if the text is not found.
Try it
Run it and change it
text = "banana"
print(text.find("na"))
Worth knowing
- find() vs index(): Use
find()when a missing substring should return-1for boolean checks; useindex()if missing values should raise aValueError.