1

I am writing a code that prompts the user to enter a sentence which is then defined as str1 and then is prompted to enter a word defined as str2.

For example:

    Please enter a sentence: i like to code in python and code things
    Thank you, you entered:  i like to code in python and code things
    Please enter a word: code

I want to use a for loop to find str2 in str1 and for it to print whether the word has/has not been found and if it has been found, the index position(s) of str2.

Currently i have this code:

    str1Input = input("Please enter a sentence: ")
    print("Thank you, you entered: ",str1Input)

    str1 = str1Input.split()

    str2 = input("Please enter a word: ")

    for eachWord in str1:
        if str2 in str1:
            print("That word was found at index position", str1.index(str2)+1)
        else:
            print("Sorry that word was not found")

Although the outcome appears to print whether or not the word was found with the index value of the word inside str1 once for each word in the sentence? For instance, if str1 was "apples oranges lemons limes pears" and i chose the word "apples" it would come up with:

    That word was found at index position: 1
    That word was found at index position: 1
    That word was found at index position: 1
    That word was found at index position: 1
    That word was found at index position: 1

If anyone could help me and anyone else attempting something similar to this that would be extremely helpful! Thanks! :)

3
  • is it just about finding the position the a search word in the given string? Commented Jan 29, 2017 at 18:38
  • Yes that is correct, also if there are 2 or more of the same word in a given string i want it to be able to print the index positions of them all Commented Jan 30, 2017 at 17:54
  • If you are splitting by spaces then how can a word be repeated in one element in the list? Unless you have a word like cancan and are searching for the word can. You also us the loop for eachWord in str1: and then never use eachWord! You are doing the same search on each iteration. Commented Jan 30, 2017 at 22:21

1 Answer 1

1

The problem with you code is that you use for eachWord in str1. This means you iterate through each character in str1 rather than every word. To solve this, use str1.split() to separate words. You also should have the if str2 in str2 outside the for loop; check if str2 is in str1, and iterate through str1 if it is, rather than iterating through str1, and checking if it contains str2 every time.As a word can appear in the sentence more than once, you won't be able to use str1.split().index() to find all the positions, as index() always returns the lowest position of an item in a list.

An easier method is to use a list comprehension:

positions=[x for x in range(len(str1.split()))if str1.split()[x]==str2]

This will contain all the indexes of str2 in str1.split().

Final code:

positions=[x for x in range(len(str1.split()))if str1.split()[x]==str2]
if positions:
    for position in positions:
        print("That word was found at index position",position)
else:
    print("Sorry that word was not found")

Input:

Please enter a sentence: i like to code in python and code things
Thank you, you entered:  i like to code in python and code things
Please enter a word: code

Output:

That word was found at index position 3
That word was found at index position 7
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply, i have edited the code using my for loop method and managed to find a way to print the index position of the word although it prints this same message for the number of index positions in the word rather than repeating it (without even the index position) for the total number of index positions in the sentence. Which is a step forward. Is there a way preferably using the for loop method that i could get it to print out the index position once and also print the index positions of the word if it is featured in the sentence more than once? My new code is in the question box

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.