-1

I already made a start to this and it works perfectly so far, however it is not finding the position of one word no matter the method I used. I have researched and have found nothing so far, please help.

print ("Enter your sentence here.")
#This will ensure that the person knows to input a sentence.
sentence = input ()
#This will allow the person to input a sentence.
s = sentence.split()
#This will allow me to split the sentence.
positions = [s.index(x)+1 for x in s]
#This will change the first position from 0 to 1.
print ("Which word would you like to find in this sentence?")
#This will allow the person to write a word in their sentence.
word = input ()
#This allows the person to input the word.
print (positions)
Skylerose
  • 13
  • 3
  • duplicate http://stackoverflow.com/questions/21842885/python-find-a-substring-in-a-string-and-returning-the-index-of-the-substring – p-j-e Feb 02 '17 at 10:26
  • 1
    Possible duplicate of [Python: Find a word within a string](http://stackoverflow.com/questions/24103522/python-find-a-word-within-a-string) – jkalden Feb 02 '17 at 10:36

2 Answers2

1
print ("Enter your sentence here.")
#This will ensure that the person knows to input a sentence.
sentence = input ()
#This will allow the person to input a sentence.
s = sentence.split()
#This will allow me to split the sentence.
print ("Which word would you like to find in this sentence?")
#This will allow the person to write a word in their sentence.
word = input ()
#This allows the person to input the word.
print(s.index(word) + 1)

You can add on the extra index position at the end.

n12312
  • 70
  • 1
  • 6
0

You can also use enumerate to print word and their position in sentence:

print "enter sentence"
sentence=raw_input()
print "enter word"
word=raw_input()
s=sentence.split()
for index, w in enumerate(s):
    if w==word:
        print w, index
Aashutosh jha
  • 552
  • 6
  • 8