# 查找子串 python25讲
s="hello andfdfdkjfklsds kjdg edu lkdjgkldjlkgjl"
head="and"
tail="sds"
i=0
h=0 #记录头的位置
t=0 #记录t的位置
while i<=len(s)-len(head):
k=0
c=0#统计匹配到的子串长度
while k<len(head):
if s[i+k]==head[k]:
c=c+1
k=k+1
ct=0
while k<len(tail):
if s[i+k]==tail[k]:
ct=ct+1
k=k+1
if c==len(head):#如果匹配到的长度等于子串长度
h=i
if ct==len(tail):
t=i
print(t)
i=i+1
print(h,t)
练习9-子串-2重循环
最新推荐文章于 2019-07-02 16:38:00 发布