Function giving wrong output in Python
<p>Through this code, i wish to replace all the dots (<code>.</code>) appearing in the string s with the character present at exactly the symmetrically opposite position. For eg: if <code>s=a.bcdcbba</code>, then the <code>.</code> should be replaced by <code>b</code></p>

<p><strong>i.e:</strong></p>

<p>The element at <code>i</code>th position should be replaced by the element at <code>len(s)-i-1</code>th position. This function gives wrong output for the cases like <code>g....</code> , <code>.g...</code> etc. Any help ?</p>

<pre><code>def replacedots(s):

  for i in range(0,len(s)):
      if s[i]==".":
              s=s.replace(s[i],s[len(s)-i-1],1)

  return s    
</code></pre>

<p>@chepner's way:</p>

<pre><code>def replacedots(s):
    return ''.join(x if x !='.' else y for x, y in zip(s, reversed(s)))
</code></pre>

<p>an alternative:</p>

<pre><code>def replacedots(s):
    return ''.join(c if c != '.' else s[-i - 1] for i, c in enumerate(s))
</code></pre>

<p>When the char at position <code>i</code> and the char at position <code>len(s) - i - 1</code> is a <code>.</code> the dot will remain a dot.</p>

<p><strong>Example:</strong></p>

<pre><code>s = "foo.bar"
</code></pre>

python