Reputation: 309
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
to print whether the word is or is not 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: ")
if str2 in str1:
for 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 index position was found but then prints the index position of every word inside the sentence. Also if I am searching of a certain word that appears twice in that sentence, it just prints the index position of the first time the word is seen in the sentence, for example:
Please enter a sentence: i like to code in python and code things
Please enter a word: code
Thank you, you entered: i like to code in python and code things
That word was found at index position: 1
That word was found at index position: 2
That word was found at index position: 3
That word was found at index position: 4
That word was found at index position: 5
That word was found at index position: 6
That word was found at index position: 7
That word was found at index position: 4
That word was found at index position: 9
If anyone could help me and anyone else attempting something similar to this that would be extremely helpful!
Upvotes: 1
Views: 11777
Reputation: 82
Just make sure to use comparisons in if/else statements and think about what your loop is doing.
Hope this is simple enough but effective:
EDIT: Add a counter rather than using ".index()". Just keeps it nice and basic:
str1In = "I like to code a few things and code a lot"
str2 = "code"
str1 = str1In.split()
indexCount = 0
for word in str1:
if word == str2:
print("Your word was found at index point",int(indexCount))
else:
print("Your word was not found at",int(indexCount))
indexCount += 1
Upvotes: 0
Reputation: 71451
What you can do is make a list out of str1 and find where str2 occurs in the list. Here is the code:
str1 = "i like to code in python and code things"
str2 = "code"
the_indexes = []
the_new_list = [str1.split(' ')]
the_count = str1.count(str2)
if the_count > 0:
for i in range(len(the_new_list[0])):
if the_new_list[0][i] == str2:
the_indexes.append(i)
else:
print str2, " not found in the first sentence"
print "The indexes are: "
for i in the_indexes:
print i
Upvotes: 0
Reputation: 192
Use the enumerate
python built-in function:
for index, word in enumerate(splitted_sentence):
if word == target_word:
print("Index: ", index)
docs: https://docs.python.org/3.3/library/functions.html#enumerate
UPD: list.index()
method returns the lowest index of matching element. That's why you always get same index if your word appears twice in a sentence.
Check the docs on this as well: https://docs.python.org/3.6/tutorial/datastructures.html#more-on-lists
Upvotes: 2
Reputation: 152637
You can use a conditional list comprehension (it's like a for
-loop):
>>> str1 = 'i like to code in python and code things'
>>> str2 = 'code'
>>> indices = [idx for idx, word in enumerate(str1Input.split(), 1) if word == str2]
>>> indices
[4, 8]
Giving you the indices of the matches. Then you can do whatever you like with those:
if indices:
for idx in indices:
print('word found at position {}'.format(idx)
else:
print('word not found')
Your attempt actually wasn't too bad but you did one mistake: for str2 in str1:
this overwrites your str2
variable which holds the string to look for! Also index
will always give you the first index of your variable so when you did str1.index(str2)+1
you looked for the first occurence of the currently investigated item! That's why you had That word was found at index position: 4
two times because it only looked for the first 'code'
.
The documentation is always useful to read:
Return the index in the list of the first item whose value is x. It is an error if there is no such item.
Upvotes: 2
Reputation: 757
Your problem is in for
loop: you assign values from str1
to local variable str1
each iteration. It's called variable shadowing.
The solution is to use different variable name in loop declaration.
Upvotes: 0