Python IndexError: string index out of range
<p>Can someone help me why is it saying: "IndexError: string index out of range"
When I add the "letterCount += 1" to the first else it makes this error, without it is working.</p>

<p>The goal is to count "bob"s in s.</p>

<p>Thanks!</p>

<pre><code>s = 'oobobodobooobobobobabobbobbobobbobbobhbxbobbk'

vowelCount = 0
letterCount = 0
pointer = s

for pointer in s:  
    print(pointer)
    if pointer == 'b':
        print (str(letterCount) + '. betÅ± B' )

        if (s[letterCount+1] + s[letterCount+2]) == str('ob') :
           vowelCount += 1
           letterCount += 1
           print( str(vowelCount) + '. BOB megtalÃ¡lva')
        else:
           print('Nem OB jÃ¶n utÃ¡na')
           letterCount += 1
    else:      
        print(str(letterCount) + '. betÅ± nem B')
        letterCount += 1

print ("Number of times bob occurs is: " + str(vowelCount))
</code></pre>

<p>I hope the following code will work for you.</p>

<pre><code>s = 'oobobodobooobobobobabobbobbobobbobbobhbxbobbk'

vowelCount = 0

letterCount = 0

pointer = s

print len(s)

for pointer in s:

    if pointer == 'b':

        if (len(s) != letterCount+1 and len(s) != letterCount+2):

            if (s[letterCount+1] + s[letterCount+2]) == str('ob'):

                vowelCount += 1

                letterCount += 1

                print(str(vowelCount) + '. BOB')

            else:

                letterCount += 1
    else:

        letterCount += 1


print ("Number of times bob occurs is: " + str(vowelCount))
</code></pre>

<p>In this statement, i'm checking with letter count  with the len of the string. it will match at the end of the string only.</p>

<p>or you can use the enumerator to check the len of the word in a string</p>

<p><pre>
for i, _ in enumerate(s): #i here is the index, equal to "i in 
range(len(s))"</p>

<code>if s[i:i+3] == 'bob': #Check the current char + the next three chars.

    bob += 1

print('Number of times bob occurs is: ' + str(bob))
</code></pre>

python