我需要写一个返回Python中的字符串中的第一子串的位置,而无需使用string.find(sub)
Python。如何编写一个函数来查找字符串中第一次出现子字符串的位置?
def sublocation(string,sub):
i=0
while i<len(string):
o=0
while o<len(sub):
u=i
if string[i] == sub[o]:
o=o+1
u=u+1
result=True
i=len(string)
else:
i=i+1-u
result=False
使用'str.index'。我会帮你的。 – 2014-11-04 09:16:12
到目前为止您尝试过了什么,为什么您需要自己编写而不是使用现有的Python函数? – nos 2014-11-04 09:17:03
欢迎来到SO,这可能会帮助你http://stackoverflow.com/help/how-to-ask – greole 2014-11-04 09:17:51