Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何纠正返回字符串中子字符串数的代码?_Python - Fatal编程技术网

Python 如何纠正返回字符串中子字符串数的代码?

Python 如何纠正返回字符串中子字符串数的代码?,python,Python,我想制作一个程序,计算字符串中给定子字符串的数量 s = 'azcbobobegghakl' substring = "bob" for i in range(0, len(s)): if substring in s: j = 0 count = 0 count1 = 0 while s[i] == substring[j]: count += 1 i += 1

我想制作一个程序,计算字符串中给定子字符串的数量

s = 'azcbobobegghakl'
substring = "bob"

for i in range(0, len(s)):
    if substring in s:
        j = 0
        count = 0
        count1 = 0
        while s[i] == substring[j]:
            count += 1
            i += 1
            j += 1
            if j > len(substring):
                break
        if count == len(substring):
            count1 += 1
            count = 0
        else:
            i += 1

print(count1)
输出:

2
2