Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8297843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:33:19+00:00 2026-06-08T15:33:19+00:00

allow me to preface this by saying that i am learning python on my

  • 0

allow me to preface this by saying that i am learning python on my own as part of my own curiosity, and i was recommended a free online computer science course that is publicly available, so i apologize if i am using terms incorrectly.

i have seen questions regarding this particular problem on here before – but i have a separate question from them and did not want to hijack those threads. the question:

“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.”

my solution (which works) is:

first = str(input())
second = str(input())

count = 0
location = 0
while location < len(second):
   if location == 0:
      location = str.find(second,first,0)
      if location < 0:
         break
      count = count + 1                          
   location = str.find(second,first,location +1)   
   if location < 0:
      break
   count = count + 1
print(count)

if you notice, i have on two separate occasions made the if statement that if location is less than 0, to break. is there some way to make this a ‘global’ condition so i do not have repetitive code? i imagine efficiency becomes paramount with increasing program sophistication so i am trying to develop good practice now.

how would python gurus optimize this code or am i just being too nitpicky?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:33:20+00:00Added an answer on June 8, 2026 at 3:33 pm

    Check out regular expressions, python’s re module (http://docs.python.org/library/re.html). For example,

    import re
    first = str(input())
    second = str(input())
    regex = first[:-1] + '(?=' + first[-1] + ')'
    print(len(re.findall(regex, second)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please allow me to preface this by saying I'm pretty much brand new to
Let me preface this by saying that I'm a very raw iPhone developer. I'm
I'm going to preface this by saying that this is by no means a
I need to preface this with my instructor won't allow us to use IDE's
I'll preface this by saying this is my first Mac application, though I've been
Before the question, let me preface this with the fact that I am working
Let me preface by I am just starting Python so if this is really
I should preface this question by saying I think the answer is probably no,
I want to preface this question with the fact that I am a student
This is a C# winforms app. Preface: I am creating a form that will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.