Read this for more on strings.
What does the following function do?
def find(word, letter): index = 0 while index < len(word): if word[index] == letter: return index index = index + 1 return -1In a sense,
find
is the inverse of the [] operator. Instead of taking an index and extracting the corresponding character, it takes a character and finds the index where that character appears. If the character is not found, the function returns -1.
return
statement inside a loop. If word[index] == letter
, the function breaks out of the loop and returns immediately. find
so that it has a third parameter, the index in word where it should start looking.