str.find
Find the first occurrence of the substring sub in the string.
Python 3.13
str.find(sub[, start[, end]])- [ ]optional part
- sub
- The substring to find.str
- start
- The index to start searching from, 0 by default; a negative one counts from the end.intOptional
- end
- The index before which the search stops, the end by default; a negative one counts from the end.intOptional
Return value
The index of the first occurrence, or -1 if the substring is not found.
Examples
Position of the first match
Python 3.13
print('hello'.find('l'))2
Substring not found
Python 3.13
print('hello'.find('z'))-1
Details
To only check whether a substring is present, use the in operator: "ll" in "hello". If a missing substring is an error, str.index is more convenient.
