Python string find() method
Return the position of the first occurrence of some text, or -1.
Syntax
str.find(sub, start=0, end=len)
Returns -1 when the text is not found (unlike index, which raises an error). To just test whether text is present, in is clearer.
Parameters
| sub | the text to look for |
| start | where to start looking |
| end | where to stop looking |
Return value
int (position, or -1)
Example
Code
email = "[email protected]" at = email.find("@") print(at, email[:at], email[at + 1:]) print("banana".find("na"), "banana".find("na", 3)) print("banana".find("x")) print("na" in "banana")
Output
4 sita example.com 2 4 -1 True
Related
Learn it step by step in the free lesson Strings, with exercises that check your answers.