find()
A string method that returns the index where a substring first appears, or -1 if it does not appear.
In more detail
s.find(sub, start) begins looking from index start, which lets you find later occurrences; rfind searches from the right. Check for -1 before using the result as an index, because -1 is a valid index meaning the last character.
Example
"Mississippi".find("ss") # 2Related terms
- string method — A method that belongs to strings, such as upper, lower, count, find, replace, split, strip, startswith or join.
- substring — A run of consecutive characters inside a longer string, such as "ssi" in "Mississippi".
- string index — The position number of a character in a string or an item in a list, counting from 0 at the left, used in square brackets as s[0].
Taught in
- String Slices — Hands-on Python: Complete Video Course & Book
- Split And Join — Hands-on Python: Complete Video Course & Book
- Mad Libs Revisited: Finding The Cues — Hands-on Python: Complete Video Course & Book