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>You need to check the string s length with something like:</p>

<pre><code>letterCount+2 &lt;= len(s) 
</code></pre>

<p>i.e.</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 (letterCount+2 &lt;= len(s) and (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>

python