# Count substring in string

With the comments on the same line as the code, it refers to. e.g

NumBob = 0
s = 'sexbobombob'
i = range(len(s)+1) #Generates a list from 0 to the length of s
u = 0 # This would be the variable storing each value of the list
for ans in i: # this allows me to extract each value of i
if ans > 0: # Completely arbitrary. i just used any condition that allows me to pick up all values in the list.
u = ans
x = s[u:u+3] #By using indexing to pick u, u+1, and u +2 i can pick up 3 letters at a time
if "bob" in x: # Checking if the term i picked up is a Bob
numBob += 1 # increasing the Bob count
print "Number of times bob occurs is:" + str(numBob) # Printing the number of Bobs


With the comments beneath the line of code, it refers to. e.g

numBob = 0
s = 'sexbobombob'
i = range(len(s)+1)
#Generates a list from 0 to the length of s
u = 0
# This would be the variable storing each value of the list
for ans in i:
# this allows me to extract each value of i
if ans > 0:
# Completely arbitrary. i just used any condition that allows me to pick up all values in the list.
u = ans
x = s[u:u+3]
#By using indexing to pick u, u+1, and u +2 i can pick up 3 letters at a time
if "bob" in x:
# Checking if the term i picked up is a Bob
numBob += 1
# increasing the Bob count
print "Number of times bob occurs is:" + str(numBob)
#Printing the number of Bobs


Or with the comments preceding the line of code. e.g

numBob = 0
s = 'sexbobombob'
#Generates a list from 0 to the length of s
i = range(len(s)+1)
# This would be the variable storing each value of the list
u = 0
# this allows me to extract each value of i
for ans in i:
# Completely arbitrary. i just used any condition that allows me to pick up all values in the list.
if ans > 0:
u = ans
# By using indexing to pick u, u+1, and u +2 i can pick up 3 letters at a time
x = s[u:u+3]
# Checking if the term i picked up is a Bob
if "bob" in x:
# increasing the Bob count
numBob += 1
# Printing the number of Bobs
print "Number of times bob occurs is:" + str(numBob)


Using this kind of all-top-level short-named fully imperative style forces you to rely heavily on comments to make the code readable.

Comments everywhere repeating the code violate the DRY principle:

    # increasing the Bob count
numBob += 1


You just doubled reading / maintaining time here.

Descriptive names, examples of usage and sometimes functional style make "how"-kind comments unnecessary:

def count(substring, string):
"""
>>> count("bob","bobhejrhaabobfqbqsanbobb")
3
"""
return sum(substring == group for group in groups(string, len(substring)))

def groups(xs, n):
"""
>>> groups([1, 2, 3, 4, 5], 3)
[[1, 2, 3], [2, 3, 4], [3, 4, 5]]
"""
for i in range(len(xs)-n):
yield xs[i:i+n]


The code looks self explaining to me, but feel free to ask about any doubts about it.

• Even simpler: def count(substring, string): return sum(string.startswith(substring, i) for i in range(len(string) - len(substring))) – 200_success Sep 20 '16 at 7:33