How to count all occurrences of a word in a string using python

后端 未结 4 1445
栀梦
栀梦 2021-01-21 04:04

I\'m trying to find the number of times \'bob\' occurs in a string of characters like \'abdebobdfhbobob\'.

My code (that I found through another stackoverflow question)

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 04:48

    why do not you make it easy?

    bobc=0
    for i in range (0,len(s)-2):
        if s[i:i+3]=='bob':
            bobc+=1
            i=+1
    print('Number of bob:'+str(bobc))
    

提交回复
热议问题