I just explain where and why the error occured.
s = 'azcbobobegghakl'
ls =[]
for x in s:
ls.append(x)
print(ls)
#equal to
#ls = list(s)
for z in ls:
count = 0
i = ls.index("b")
print(z) # !!!! z just a single letter,you can not use index on it !!!
if z[i] == "b":
if z[i+1] == "o":
if z[i+2] == "b":
count +=1
follow your idea,i think you want write like this:
But it is not right,because i = ls.index("b")
never change,you match a same word 15 times
s = 'azcbobobegghakl'
ls =[]
for x in s:
ls.append(x)
print(ls)
ls = list(s)
for z in ls:
count = 0
i = ls.index("b")
print(z) # z just a single letter,you can not use index on it
if ls[i] == "b": #but ls can do this
if ls[i+1] == "o":
if ls[i+2] == "b":
count +=1
print(count)
Be brief.
import re
s = 'azcbobobegghakl'
print(len(re.findall("b(?=ob)",s)))