Skip to main content
Engineering LibreTexts

21.6: Searching

  • Page ID
  • 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 -1


    This is the first example we have seen of a return statement inside a loop. If word[index] == letter, the function breaks out of the loop and returns immediately.

    If the character doesn’t appear in the string, the program exits the loop normally and returns -1.

    This pattern of computation—traversing a sequence and returning when we find what we are looking for—is called a search.

    Exercise \(\PageIndex{1}\)

    Modify find so that it has a third parameter, the index in word where it should start looking.

    • Was this article helpful?