【发布时间】:2019-03-12 10:10:53
【问题描述】:
我正在尝试计算一个序列在给定字符串中出现的次数。
def count_seqence(str, seq):
count = 0
if seq in str:
count += 1
return count
print(count_seqence("the quick brown fox jumps over the lazy dog","he"))
但这只运行一次并且不循环,我如何循环并计算出现的次数,因为我知道循环将针对每个字符而不是序列,这让我感到困惑。
【问题讨论】:
-
mystr.count('he')?
标签: python python-3.x string iteration sequence