Python 28

def strStr(haystack, needle):
    if not needle:
        return 0

    for index, value in enumerate(haystack):
        if value==needle[0] and haystack[index:len(needle)+index]==needle:
            return index
    return -1
	
print strStr("hello", "ll")
print strStr("aaaaa", "bba")


结果

G:\>python 28.py
2
-1

G:\>


你可能感兴趣的:(Python 28)