How do i return the position of a word that appears twice in a string?
<p>I'm writing a program where the user has to enter a set of string characters. They then pick a keyword that may or may not be in the string. If it is then the program will run through the string and see how many times the keyword appear and it will print this to the screen. I have done this so it does it but if the keyword appears twice. How do I get it so if the word appears twice then the program will print all positions of it? </p>

<p>This is what i have so far:</p>

<pre><code>#Start by making a string
String = input("Please enter a set of string characters.\n")

#Make the user choose a keyword
Keyword = input("Please enter a keyword that we can tell you the position of.\n")

#Split the string into single words assigning the position to the word after the space
IndivualWords = String.split(' ')

#Start an IF statement 
if Keyword in IndivualWords:

    #If the IF is true then access the index and assign the keyword a position 
    pos = IndivualWords.index(Keyword)

    #Print the position of the word
    print (pos +1)

else:

    #Print an error
    print("That word is not in the string.")
</code></pre>

<p>You could use <a href="https://docs.python.org/2/library/re.html#re.finditer" rel="nofollow">re.finditer</a>, here's a little example out of your example:</p>

<pre><code>import re

sentence = input("Enter a set of string characters:")
keyword = input("Enter a keyword that we can tell you the position of:")

for m in re.finditer(keyword, sentence):
    print('{0} found {1}-{2}'.format(keyword, m.start(), m.end()))
</code></pre>

python string