Counting matching substrings in a string
<pre><code>s = "abobabobabob"
total = 0

for i in range(len(s)):
    if s[i-1 : i+2] == 'bob':
        total += 1

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

<p>Is there a simpler way to change the <code>if</code> statement? Also, could someone tell me what <code>i-1 : i+2</code> does?</p>

<p>I wrote this code to find the occurrences of "bob" and I am stuck for a while.</p>

<p>Your if-statement is looking at a subset of s. To sort of answer your other question, here's a simpler approach that changes more than the if-statement:</p>

<p>This is python's regular expression library</p>

<pre><code>import re
</code></pre>

<p>The embedded statement searches for all, non-overlapping instances of 'bob' and returns a list with each match as an element; the outer statement just counts the number of elements in the list</p>

<pre><code>len(re.findall('bob',s))
</code></pre>

python