954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

function to take in a word and sub-string as arguments...

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~

pwolf
Junior Poster in Training
71 posts since Dec 2011
Reputation Points: 10
Solved Threads: 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.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: