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>Thanks to @Patrick for pointing out the flaw in my previous answer.</p>

<hr>

<p>Instead of using <code>range()</code>, just use list comprehension and string indices:</p>

<pre><code>url = 'www.google.com/bla.bla'
url = url[:15] + ''.join([c for c in url[15:] if c!= '.'])
</code></pre>

<p>Explanation.</p>

<ul>
<li><code>url =</code>: reassign the value of url to...</li>
<li><code>url[:15]</code>: all the characters in <code>url</code> before the last seven...</li>
<li><code>+</code>: plus..</li>
<li><code>''.join([c for c in url[15:] if c!= '.'])</code>: the last seven characters in the string <code>url</code>, <strong>only if</strong> the value is not a <code>.</code>. Then join the list of characters together into a string.</li>
</ul>

<p>After being modified the value of url will be: <code>'www.google.com/blabla'</code></p>

<p><code>print("Hello World") #keywords such as and or as are still highlighted</code></p>

python string