Find the word from a given sentence having given word as prefix | Function to find the position of the string having word as prefix ; Initialize an List ; Extract words from the sentence ; Traverse each word ; Traverse the characters of word ; If prefix does not match ; Otherwise ; Return the word ; Return - 1 if not present ; Driver code
def isPrefixOfWord ( sentence , word ) : NEW_LINE INDENT a = sentence . split ( " ▁ " ) NEW_LINE v = [ ] NEW_LINE for i in a : NEW_LINE INDENT v . append ( i ) NEW_LINE DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT for j in range ( len ( v [ i ] ) ) : NEW_LINE INDENT if ( v [ i ] [ j ] != word [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT elif ( j == len ( word ) - 1 ) : NEW_LINE INDENT return v [ i ] NEW_LINE DEDENT DEDENT DEDENT return - 1 NEW_LINE DEDENT s = " Welcome ▁ to ▁ Geeksforgeeks " NEW_LINE word = " Gee " NEW_LINE print ( isPrefixOfWord ( s , word ) ) NEW_LINE