简体   繁体   中英

Finding values in a string using for loops in Python 3

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! :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM