May I suggest you use regular expressions?

You can check it out here (take note of the difference between search,
findall etc):
https://docs.python.org/3/library/re.html

Essentially it is a standard library built for matching patterns in
strings. It is a powerful tool that is kind of it's own language in itself
(but other languages have re libraries too so it's definitely worth
learning). The magic of re is learning how to construct your patterns.

re.findall()  will return all non-overlapping matches of a pattern in a
given string.  However, the pattern I've used below will match overlapping
bobs, this is because of the lookahead assertion (the "?=" part of the
pattern). I won't go into regex anymore, it's something I believe you learn
as you do otherwise its way too syntax to learn at once.

 DISCLAIMER: I have only just learnt regex myself lately so if anyone wants
to charm in with a better method please to do so!

Here is a snippet which solves your problem

#!/usr/bin/env python3

import re

pat = r'(?=(bob))'
s = 'azcbobobegghakl'

result = re.findall(pat, s)

print(result)
print(len(result))



On 6 September 2017 at 15:49, Cameron Simpson <c...@cskk.id.au> wrote:

> On 05Sep2017 22:34, Pat Martin <wpmar...@gmail.com> wrote:
>
>> I am trying to write a program for a programming class that finds the
>> number of a specific string (bob) in a string of characters. I am using
>> one
>> of the sample strings they give me and it should find 2 instances of bob
>> but my script returns 0. Since they want it to find 2 from the bobob in
>> the
>> string using "bob in s" doesn't work (it only returns 1). My eyes are
>> crossing looking at the code, can someone give me a hint on what I am
>> missing that causes this to not give me the correct answer of 2.
>>
>> #!/usr/bin/env python3
>>
>> s = 'azcbobobegghakl'
>>
>> count = 0
>> theo = False
>> firstb = False
>>
>> for i in s:
>>    if i == 'b':
>>        firstb == True
>>
>
> This line is a boolean expression using "==", not an assignment using "=".
> As a consequence firstb is never set to True and the rest of the logic
> never fires.
>
> It is legal in Python to just put an expression on a line.
>
> It is usually worth putting in print() calls to debug things like this. I
> put:
>
>  print(i, firstb, theo, count)
>
> at the start of the loop and learned that firstb never becomes true. I
> also put the same print statement at the bottom.
>
> The advantage of hte above print statement is that it shows you the
> character from the string along with each set of values. That way you can
> scan down the output to the "bob" part and look for correct behaviour.
>
> I that hadn't helped I've have stuck print after each "if" until the
> problem became glaringly obvious.
>
> BTW, it isn't good to write tests like:
>
>  if theo == True:
>
> "theo" a Boolean anyway. Just say:
>
>  if theo:
>
> It reads more naturally as well.
>
> Cheers,
> Cameron Simpson <c...@cskk.id.au> (formerly c...@zip.com.au)
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Best Regards,

Ben Southwell
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to