title: IndexError: string index out of range on equation cleaning function
body: <p>I have code to append <code>^0</code> to all the constants in my code so that if you had the string "3x^2+14+2" it would become "3x^2+14^0+2^0" however I am getting a IndexError and I have no idea what I am doing wrong. This is my code:</p>
  
  <pre><code>def cleanEquation(equation):
      equation = ''.join(equation.split())
      for i in range(len(equation)):
          if equation[i].isdigit():
              if equation[i-1] != "^":
                  if i == len(equation)-1:
                      equation = equation[:i+1] + '^0'
                  if equation[i+1] == "+" or equation[i+1] == "-":
                      equation = equation[:i+1] + '^0' + equation[i+1]
  
  cleanEquation("x+14+y+14")
  </code></pre>
  
  <p>Whenever I try to run this I get:</p>
  
  <pre><code>IndexError: string index out of range
  </code></pre>
  
  <p>This is only a snippet of the function the whole function adds 1 to the beggining of every coefficient and also adds ^1 to every variable with no coefficient and those 2 parts work fine for some reason even though they have the same format is this part of the function. I can post the full function if needed.</p>