Write a program that prints the number of times the string contains a substring
<pre><code>s = "bobobobobobsdfsdfbob"
count = 0 
for x in s : 
    if x == "bob" : 
       count += 1
print count
</code></pre>

<p>i want to count how many bobs in string s, the result if this gives me 17 
what's wrong with my code i'm newbie python.</p>

<p>I'm not giving the best solution, just trying to correct your code.  </p>

<p><strong>Understanding what <code>for each (a.k.a range for)</code> does in your case</strong>  </p>

<pre><code>for c in "Hello":
    print c  
</code></pre>

<p>Outputs:  </p>

<pre><code>H
e
l
l
o
</code></pre>

<p>In each iteration you are comparing a character to a string which results in a wrong answer.<br>
Try something like<br>
(For no overlapping, i.e no span)    </p>

<pre><code>s = "bobobobobobsdfsdfbob"
w = "bob"
count = 0
i = 0
while i &lt;= len(s) - len(w):
    if s[i:i+len(w)] == w:
        count += 1
        i += len(w)
    else:
        i += 1
print (count)
</code></pre>

<p>Output:</p>

<p><code>Count = 4</code></p>

<p>Overlapping    </p>

<pre><code>s = "bobobobobobsdfsdfbob"
w = "bob"
count = 0
for i in range(len(s) - len(w) + 1):
    if s[i:i+len(w)] == w:
         count += 1
print (count)
</code></pre>

<p>Output:<br>
<code>Count = 6</code></p>

python python-2.7 python-3.x