Using replace instead of find
<p>I have written the following piece but not able to figure the next part:</p>

<pre><code>if request.guess in game.target:            
  position = game.target.find(request.guess)
  game.correct[position] = request.guess.upper()
</code></pre>

<p>The point here is <code>request.guess</code> is a character input like <code>'a'</code> and <code>game.target</code> is <code>'austin'</code>. So the above code will find the character in the target word and generate an output like <code>a*****</code>. But my code will only work for words that have letters not repeated in them eg: Australia. I know there is a method 'replace' in python but I am not able to figure out how should I incorporate here.</p>

<p>Kindly help!!!!!</p>

<p>I'd suggest you invest some time in learning <a href="https://docs.python.org/2/library/re.html" rel="nofollow">regular expressions</a>, they come in handy in many similar situations and are often more expressive (and efficient) than ad-hoc implementations. Plus you can reuse (with some quirks) your understanding of them in other languages and OS shells, which makes them very powerful.</p>

<p>You can build a regular expression from your pattern that will match anything except the characters you are looking for: <code>r'[^{}]'.format(pattern)</code>; then use it to do the substitution:</p>

<pre><code>import re
pattern = request.guess
result = re.sub(r'[^{}]'.format(pattern),"*","adam")
</code></pre>

<p><a href="https://ideone.com/7eDYFz" rel="nofollow">Example on IdeOne</a>:</p>

<pre><code>import re
print re.sub(r'[^{}]'.format('ad'),"*","adam")
print re.sub(r'[^{}]'.format('a'),"*","adam")
print re.sub(r'[^{}]'.format('m'),"*","adam")
</code></pre>

<p>Prints:</p>

<pre><code>ada*
a*a*
***m
</code></pre>

python python-3.x