Apr-05-2018, 08:16 PM
I am participating in the online course.
Its my first week with limited 1hour/day time for learning so please be understanding.
My question concerns the condition if in the code below. Initially I thought that "if" would concern the length if the string "s" but when I change the value to "if s[:i]" it returns 0 occurences of 'bob'. I simply thought that I would just entered the "s" string length...
Please explain why? What am I missing?
ans=0
s = 'jgguubobxggbobcfybobbob'
for i in range (len(s)):
if s[i:i+3] == 'bob':
ans += 1
print("Number of times bob occurs is: ",ans)
Its my first week with limited 1hour/day time for learning so please be understanding.
My question concerns the condition if in the code below. Initially I thought that "if" would concern the length if the string "s" but when I change the value to "if s[:i]" it returns 0 occurences of 'bob'. I simply thought that I would just entered the "s" string length...
Please explain why? What am I missing?
ans=0
s = 'jgguubobxggbobcfybobbob'
for i in range (len(s)):
if s[i:i+3] == 'bob':
ans += 1
print("Number of times bob occurs is: ",ans)