Why am I getting this "IndexError: string index out of range" in one case but not the other?
<pre><code>#i couldnt find the difference in the code 
    &gt;&gt;&gt; def match_ends(words):
     # +++your code here+++
     count=0
     for string in words:
      if len(string)&gt;=2 and string[0]==string[-1]:
       count=count+1
     return count

&gt;&gt;&gt; match_ends(['', 'x', 'xy', 'xyx', 'xx'])
2
&gt;&gt;&gt; 
&gt;&gt;&gt; def match_ends(words):
    # +++your code here+++
     count=0
     for string in words:
      if string[0]==string[-1] and len(string)&gt;=2:
       count=count+1
     return count

&gt;&gt;&gt; match_ends(['', 'x', 'xy', 'xyx', 'xx'])

   Traceback (most recent call last):
   File "&lt;pyshell#26&gt;", line 1, in &lt;module&gt;
   match_ends(['', 'x', 'xy', 'xyx', 'xx'])
   File "&lt;pyshell#25&gt;", line 5, in match_ends
   if string[0]==string[-1] and len(string)&gt;=2:
   IndexError: string index out of range
</code></pre>

<p>I couldn't find the difference in the code except the if condition <code>if len(string)&gt;=2 and string[0]==string[-1]:</code> in the first function   and  <code>if string[0]==string[-1] and len(string)&gt;=2:</code> in the second function</p>
