-4
s="hellohel"
sub="hel"
count=0
for i in s:
    if sub in s:
        present = True
        count+=1
    else:
        present = False

I am getting output: True 8 The output should be:True 2

print(present,count)

3 Answers3

1

Your code currently checks, for each character in hellohel, if hel is in hellohel. As it stands, hel is always in hellohel, so that will always be true. That means, your count will in practice count how many characters there are in hellohel.

Perhaps what you're looking for is something along the lines of

s = "hellohel"
sub = "hel"
count = 0
present = False
for i in range(len(s)):
    if (s[i:i+len(sub)] == sub):
        count += 1
        present = True
print(count, present) # Prints 2 True   

This code needs to be cleaned up somewhat, and there are some optimization that can be done. However, this should help push you in the right direction.

Henrik Klev
  • 322
  • 1
  • 11
  • one of the optimizations is `present = count > 0`. –  Dec 30 '21 at 14:45
  • There's an entire sub-field in computer science dedicated to text-search, and it even has its own [Wikipedia-article](https://en.wikipedia.org/wiki/String-searching_algorithm). You can take some optimization-principles from algorithms such as the [Boyer-Moore](https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string-search_algorithm) algorithm, which is frequently taught in universities. Another simple optimization includes checking whether the substring is longer than the remaining text to search. – Henrik Klev Dec 30 '21 at 15:30
0
s="hellohel" 
sub="hel"
count=0
for i in s: # this line iterates through each character the string "s" (8 times), assigning the current character to i
    if sub in s: # this line is just checking whether "hel" is in "hellohel" once for every character in "hellohel"
        present = True
        count+=1 # that means it will count to 8 every time, because "hel" is always in "hellohel"
    else:
        present = False

Overall, not the correct way to approach it. There are multiple answers here that don't use .count() but that use re, or nothing but vanilla python. Count number of occurrences of a substring in a string

yuuuu
  • 738
  • 3
  • 11
  • I feel you should have put a correction as well. – Beulah Akindele Dec 30 '21 at 16:49
  • @BeulahAkindele I could have copied an example from the thread I linked, but generally speaking thats not the done thing on here. – yuuuu Dec 30 '21 at 16:55
  • I'm just saying, you took the time to comment the code. They already said they didn't want to use the `count` function, you should have just written the correction. I upvoted your answer, cause I didn't see it should be downvoted... but your answer is technically incomplete. – Beulah Akindele Dec 30 '21 at 16:59
  • Technically I should have just flagged the post as a duplicate, as there are multiple questions quickly available from googling that have multiple answers that do not use the count function. The accepted answer is even copy pasted from one of those answers. I figured I would at least explain why what they did wasn't working, rather than just unhelpfully flag their post. I will say that the wording of my answer may have caused some confusion, as it seemed that I was linking to the other question *because* it used count. – yuuuu Dec 30 '21 at 17:02
0

So the reason why it showing is because of the for loop "for i in s:", as your string has 8 "hellohel" places. So whenever you try to run the for loop it runs 8 times.