title: Issues about if-statement and empty string in Python
body: <p>I am learning how to use python, and yesterday I got a question which is described like this:</p>
  <p>Modify the first_and_last function so that it returns True if the first letter of the string is the same as the last letter of the string, False if they’re different.</p>
  <p>By trying to solve this, I found that there are 2 almost same program, but one can run correctly, the other calls an error: <strong>string index out of range</strong></p>
  <p><strong>Program 1</strong> (This can run correctly)</p>
  <pre><code>def first_and_last(message):
      if len(message) == 0 or message[0] == message[-1]:
          return True
      else:
          return False
  
  print(first_and_last(&quot;else&quot;))
  print(first_and_last(&quot;tree&quot;))
  print(first_and_last(&quot;&quot;))
  </code></pre>
  <p><strong>Program 2</strong>(This calls an error)</p>
  <pre><code>def first_and_last(message):
      if message[0] == message[-1] or len(message) == 0:
          return True
      else:
          return False
  
  print(first_and_last(&quot;else&quot;))
  print(first_and_last(&quot;tree&quot;))
  print(first_and_last(&quot;&quot;))
  </code></pre>
  <p>I just want to know what's the difference between these 2 programs, and why they return the different results. Appreciate it for your help!</p>