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')
Author by
Patrick Hayes
Updated on November 25, 2022Comments
-
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 over 10 yearsAhh why noone uses regex?
len(re.findall("(?=bob)", "gfdhbobobyui"))
-
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 over 10 yearsactually, since you're only comparing the string,
len(s)
is sufficient. -
aIKid over 10 yearsAlso,
str.count()
doesn't work with overlapping occurences :) -
Games Brainiac over 10 years@aIKid You'll gen an index error.
-
aIKid over 10 yearsNope. I didn't. See my example, i copy pasted it.
-
Games Brainiac over 10 years@aIKid Well thats strange, it seems that slicing out of bounds raises no exception.
-
aIKid over 10 yearsYep. I'm curious of that too actually HAHAHA. But it just works.
-
Patrick Hayes over 10 yearsThank you, what is enumerate?
-
Maxime Chéramy over 10 years@GamesBrainiac Yep, there is no index error. However, why stopping at
len(s)-1
? -
aIKid over 10 yearsIt returns a tuple of the index and the value. So i'm catching it with two variables,
v
is for the current value, andi
is for the current index. -
aIKid over 10 yearsFor details, see the docs :)
-
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 over 10 yearsOK thank you that makes since!
-
Maxime Chéramy over 10 years@alKid exactly. So you can stop even earlier ;). That's what I'm saying.
-
Patrick Hayes over 10 yearsI'll check that out appreciate it!
-
Maxime Chéramy over 10 yearsI'd suggest you to write
for i, _ in enumerate(s)
since you don't usev
. Also,enumerate
might be slightly slower than usingrange(len(s))
and you also do 2 extra unnecessary iterations. -
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 over 10 years@Maxime Thanks. Edited