Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Substring Counting
#1
I am working through the Computer Science Circles Web lesson for learning Python and I am stuck on one exercise. Here are the directions:

As mentioned in lesson 7A, a substring is any consecutive sequence of characters inside another string. The same substring may occur several times inside the same string: for example "assesses" has the substring "sses" 2 times, and "trans-Panamanian banana" has the substring "an" 6 times. Write a program that takes two lines of input, we call the first needle and the second haystack. Print the number of times that needle occurs as a substring of haystack.

Here is what I have so far:

needle = input()
haystack = input()
for s in range (0, len(haystack)):
x = 0
haysub = haystack[s : len(needle) + s]
if haysub == needle:
x = x + 1
if haysub != needle:
continue
print(x)
Reply
#2
Welcome to the forum, please use code tags in the future around your code (the python button you see when posting).

Seems your answer is almost correct except you are setting x to 0 every time through the loop.
With that outside the loop it seems to work:
x = 0
for s,_ in enumerate(haystack):
    haysub = haystack[s:len(needle) + s]
    if haysub == needle:
        x += 1

print(x)
Reply
#3
Thanks Mekire.

I am very new and I don't quite understand what you mean by the tags. Could you explain that in a bit more depth?
Reply
#4
It is a way to edit post so Python code, or outputs, or errors... are more readable. Mekire used Python code tags with the code he posted in his reply to you. You can read more about it here:
https://python-forum.io/misc.php?action=help&hid=25
Reply
#5
(Jan-20-2018, 04:36 AM)Mekire Wrote:
x = 0
for s,_ in enumerate(haystack):
    haysub = haystack[s:len(needle) + s]
    if haysub == needle:
        x += 1

print(x)

I seem to be not understanding something with how the loops work. Could you please elaborate in a simple manner why in line 4 you've used the +s part?

Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 547 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,073 Apr-08-2023, 06:36 PM
Last Post: Winfried
  ValueError: substring not found nby2001 4 7,942 Aug-08-2022, 11:16 AM
Last Post: rob101
  Match substring using regex Pavel_47 6 1,433 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  Python Substring muzikman 4 2,325 Dec-01-2020, 03:07 PM
Last Post: deanhystad
  Removing items from list if containing a substring pythonnewbie138 2 2,210 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138
  Substring and If then Condition to create column Chandan 2 2,363 Jan-23-2020, 08:40 AM
Last Post: buran
  ValueError: substring not found hoangthai10788 2 4,606 Sep-23-2019, 05:34 PM
Last Post: ichabod801
  Substring extraction nevendary 6 3,961 Apr-24-2019, 05:41 AM
Last Post: nevendary
  substring between substrings Skaperen 5 4,156 Oct-27-2018, 08:45 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020