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>With regular expressions:</p>

<pre><code>import re
correct = re.sub(r'[^a]',"*","australia")  # returns "a****a**a"
</code></pre>

<p>This the regular expression <code>[^a]</code> matches all characters except "a". The <code>re.sub</code> function replaces each "not a" with a "*"</p>

<p><em>edit</em></p>

<p>Your comments make be think that you are trying to change the <code>game.correct</code> string by replacing characters in that string. Remember that strings in python are immutable. You can't change individual letters in them. So rather than try to change the stars to "a", you have to make a new string. Regular expressions are one way to do this.</p>

python python-3.x