Python: "IndexError: string index out of range" Beginner
<p>I know, I know, this question has been asked plenty of times before. But I can't figure out how to fix it here - in this particular instance. When I subtract 2, which is what was recommended, I still get the same error within if statement. Thanks 
The code (at least it should) take a string "s" and measure it against the alphabet "order" and then give an output of the longest substring in s which is in alphabetical order.</p>

<pre><code>order = "abcdefghijklmnopqrstuvwxyz"
s = 'abcbcdabc'
match = ""

for i in range(len(s)):
for j in range(len(order)):
    if (((i + j ) - 2) &lt; len(order) and order[i] == s[j]):
        match += s[i] 

print("Longest substring in alphabetical order is: " + match)
</code></pre>

<p>That is because you are using index <code>j</code> of <code>order</code> list to access <code>s</code> list. It is possible that <code>j</code> is greater than <code>len(s)</code> hence the <code>IndexError</code>.</p>

<p>I don't know what you are trying to achieve with the code. But in any case heres what you can change to make it working: <code>match += s[i]</code> OR <code>match += order[j]</code></p>

python for-loop indexing