Is str.replace() or combining two slices a better way to remove a character?
<p>I am using CodingBat for practice exercises for the Python Language, I got to the "Warmup-1 > missing_char" exercise. The problem is as so:</p>

<blockquote>
  <p>Given a non-empty string and an int n, return a new string where the
  char at index n has been removed. The value of n will be a valid index
  of a char in the original string (i.e. n will be in the range
  0..len(str)-1 inclusive).</p>
</blockquote>

<p>The examples were:</p>

<blockquote>
  <p>missing_char('kitten', 1) â 'ktten'</p>
  
  <p>missing_char('kitten', 0) â 'itten'</p>
  
  <p>missing_char('kitten', 4) â 'kittn'</p>
</blockquote>

<p>I wrote:</p>

<pre><code>def missing_char(str, n):
  return str.replace(str[n], "") 
</code></pre>

<p>It returned all correct. As I usually do when I get my answers correct, I look and see how CodingBat answered it. They answered it in the following way:</p>

<pre><code>def missing_char(str, n):
  front = str[:n]   # up to but not including n
  back = str[n+1:]  # n+1 through end of string
  return front + back
</code></pre>

<p>Now, my question is the following: which answer is more viable? Is there setbacks to my method whereas CodingBat's method is more solid? Or is this just one of those "multiple approaches to things" kind of situation?</p>

<p>For the sake of posting a formal answer and as all previous comments suggest:</p>

<p><code>str.replace(char,"")</code>  will remove all occurences of the character regardless of their position.
<code>str[:n]+str[n+1:]</code> will only remove the n-th character.</p>

<p>So for that exercice  <code>str.replace(char,"")</code> is <strong>not</strong> suitable.</p>

python