Python Slicing 'bob' in s

11,304

Solution 1

Here, try this, handcrafted :)

for i, _ in enumerate(s): #i here is the index, equal to "i in range(len(s))"
    if s[i:i+3] == 'bob': #Check the current char + the next three chars.
        bob += 1
print('Number of times bob occurs is: ' + str(bob))

Demo

>>> s = 'gfdhbobobyui'
>>> bob = 0
>>> for i, v in enumerate(s): #i here is the index, equal to "i range(len(s))"
    if s[i:i+3] == 'bob': #Check the current char + the next two chars.
        bob += 1


>>> bob
2

Hope this helps!

Solution 2

x is a number, it can't be equal to 'bob'. That's why it always output 0.

You should use x in order to get a substring of s:

bob = 0
for x in range(len(s) - 2):
    if s[x:x+3] == 'bob':
        bob += 1

You can also use enumerate.

Solution 3

s = 'gfdhbobobyui'
bob = 0
for x in range(len(s)):
     if s[x:x+3] == 'bob':  # From x to three 3 characters ahead.
         bob += 1
print('Number of times bob occurs is: ' + str(bob))

Working example.

But the best way is this, however it does not work with overlapping strings:

s.ount('bob')
Share:
11,304
Patrick Hayes
Author by

Patrick Hayes

Updated on November 25, 2022

Comments

  • Patrick Hayes
    Patrick Hayes over 1 year
        s = 'gfdhbobobyui'
        bob = 0
        for x in range(len(s)):
             if x == 'bob':
                 bob += 1
        print('Number of times bob occurs is: ' + str(bob))
    

    Attempting to write a code that will count the amount of times 'bob' appears in s, but for some reason this always outputs 0 for number of 'bob'.

    • JadedTuna
      JadedTuna over 10 years
      Ahh why noone uses regex? len(re.findall("(?=bob)", "gfdhbobobyui"))
    • aIKid
      aIKid over 10 years
      @Vik2015 I don't know why, but i studied regex for a few pages and i found out that i hate it..
  • aIKid
    aIKid over 10 years
    actually, since you're only comparing the string, len(s) is sufficient.
  • aIKid
    aIKid over 10 years
    Also, str.count() doesn't work with overlapping occurences :)
  • Games Brainiac
    Games Brainiac over 10 years
    @aIKid You'll gen an index error.
  • aIKid
    aIKid over 10 years
    Nope. I didn't. See my example, i copy pasted it.
  • Games Brainiac
    Games Brainiac over 10 years
    @aIKid Well thats strange, it seems that slicing out of bounds raises no exception.
  • aIKid
    aIKid over 10 years
    Yep. I'm curious of that too actually HAHAHA. But it just works.
  • Patrick Hayes
    Patrick Hayes over 10 years
    Thank you, what is enumerate?
  • Maxime Chéramy
    Maxime Chéramy over 10 years
    @GamesBrainiac Yep, there is no index error. However, why stopping at len(s)-1 ?
  • aIKid
    aIKid over 10 years
    It returns a tuple of the index and the value. So i'm catching it with two variables, v is for the current value, and i is for the current index.
  • aIKid
    aIKid over 10 years
    For details, see the docs :)
  • aIKid
    aIKid over 10 years
    @Maxime since if you're on the -1th index, it's impossible to have a three character word, isn't it? =)
  • Patrick Hayes
    Patrick Hayes over 10 years
    OK thank you that makes since!
  • Maxime Chéramy
    Maxime Chéramy over 10 years
    @alKid exactly. So you can stop even earlier ;). That's what I'm saying.
  • Patrick Hayes
    Patrick Hayes over 10 years
    I'll check that out appreciate it!
  • Maxime Chéramy
    Maxime Chéramy over 10 years
    I'd suggest you to write for i, _ in enumerate(s) since you don't use v. Also, enumerate might be slightly slower than using range(len(s)) and you also do 2 extra unnecessary iterations.
  • Maxime Chéramy
    Maxime Chéramy over 10 years
    @GamesBrainiac You have updated your answer but forgot to change the code on Ideone which was incorrect (didn't work with gfdhbobob because of the -3 instead of -2).
  • aIKid
    aIKid over 10 years
    @Maxime Thanks. Edited