how do i replace a character in a string by range?
<p>For example:</p>

<pre><code>url = 'www.google.com/bla.bla'
</code></pre>

<p>I need to replace '.' with '' in the last 7 characters 
==> 'www.google.com/blabla'</p>

<p>I have tried :</p>

<pre><code>for i in range(15,22):
   if url[i]=='.':
       url = url.replace('.', "")
</code></pre>

<p>But i get this error:</p>

<blockquote>
  <p>IndexError: string index out of range</p>
</blockquote>

<p>You need to change your <code>if</code> line since a single <code>=</code> is for assignment not comparison:</p>

<pre><code>if url[i] == '.':
</code></pre>

<p>And note then when using <code>replace()</code> you will need to update the original string (<code>url</code>) since <code>replace()</code> will return a new string, not update the existing string.</p>

<p>However, I think <strong>@Patrick Haugh's</strong> one line answer is the better solution, though I would modify it as follows (if you are always using a google address):</p>

<pre><code>url = url[:15] + url[15:].replace('.', '')
</code></pre>

python string