猿问
python 字符串统计
给定某一长字符串s='xxxxbobobxxxx',试判断ss='bob'在s中出现的次数。结果为2
小萝卜腿
浏览 1628
回答 2
2回答
Jeffacode
def frequency(s1, s2): count = 0 while s1: if s1.find(s2) == -1: break count += 1 s1 = s1[s1.find(s2) + len(s2):] return count if __name__ == '__main__': s1 = "xxxxbobobxxxx" s2 = "bob" print("%s occurs %d times in %s" % (s2, frequency(s1, s2), s1))
1
0
1
小萝卜腿
#已经解决了 def finds(s1, s2): i = 0 count = 0 while(i<len(s2) - len(s1) + 1): if(s1 == s2[i:i+3]): count+=1 i+=1 return count s1 = 'bob' s2 = 'azcbobobegghakl' print ("Number of times bob occurs is: "+str(finds(s1, s2)))
1
0
0
随时随地看视频
慕课网APP
相关分类
Python
我要回答