Question
This is a project that I'm working on for a class I'm taking this summer. I have some code written but I'm having a hard
This is a project that I'm working on for a class I'm taking this summer. I have some code written but I'm having a hard time understanding how to use the find function to slice the string and get an output. I'll post the progress that I have below the actual question. Any kind of help or suggestions you could give me would be greatly appreciated. Thank you in advance!
Write a code that acts based on the following steps:
Step 1: Asks the user to enter the main string
Step 2: Ask the user to enter a smaller string which needs to be found in the main string
Step 3: Search for the second string in the main string. If it was not found, display a proper message that the string was not found.
Step 4: If the string was found, display a part of the main string which includes the second string plus 5 characters before and after its occurrence.
For example, if the main string is "The quick brown fox jumps over the lazy dog." and the second string is "fox", the code should display rown fox jump
"rown" and "jump" are each 4 characters and including the space after "rown" and before "jump" the code is displaying the second string "fox" plus 5 characters before it and 5 characters after it.
def main():
main_string = input('Enter a sentence: ')
small_string = input('Enter one word from the sentence typed: ')
if small_string in main_string:
index = main_string.find(small_string)
new_string =
print(new_string)
main()
If I'm thinking correctly, I need to use find to figure out where in the main string the small string is. I don't know how to write the new string to get a display. Am I on the right track?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started