0

Write the function search(word, substring) that takes in a word and a substring as arguments and returns the position (0 indexed) of the substring if it is found in the word. The function returns -1 if the substring is not found.

is there a method to do this? i tried;

def search(word, substring):
    if substring in word:
        return 1
    else:
        return -1

but on one of their tests it returned false, because i misunderstood the question i imagine. so how should i go about this problem?

thank you for any help~

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by pyTony
0

There is built in find method, which I think you should implement yourself as exercise. Use loop to check for every possible starting place. Initialize the result variable to -1.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.