I don't understand why i get difference answer by change put line if on in code
<p>I would write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example <code>s = 'azcbobobegghakl'</code>
will print out <code>'beggh'</code>.</p>

<pre><code>s = 'azcbobobegghakl'
result = ''
word ='a'
sen = ''
for alpha in s:
  if alpha &lt; word:
        word ='a'
        sen = ''   
  if alpha &gt;= word:   
        word = alpha
        sen += alpha
        if len(result) &lt;len(sen):
            result = sen

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

<p>Answer is </p>

<pre><code>Logest substring in alphabetical order is: beggh
</code></pre>

<p>But just put code on different line. I got a wrong answer!!!</p>

<pre><code>s = 'azcbobobegghakl'
result = ''
word ='a'
sen = ''
for alpha in s:  
  if alpha &gt;= word:   
       word = alpha
       sen += alpha
       if len(result) &lt;len(sen):
           result = sen
  if alpha &lt; word:
     word ='a'
     sen = '' 
print('Longest substring in alphabetical order is: '+ result)
</code></pre>

<p>Answer </p>

<pre><code>Longest substring in alphabetical order is: eggh
</code></pre>

<p>The problem drive me mad!!!</p>

<p>You are discarding the first character of the new sequence</p>

<pre><code>s = 'azcbobobegghakl'
result = ''
word ='a'
sen = ''
for alpha in s:  
  print alpha,word,sen
  if alpha &gt;= word:   
       word = alpha
       sen += alpha
       if len(result) &lt;len(sen):
           result = sen
  if alpha &lt; word:
     word = alpha
     sen = alpha 
print('Longest substring in alphabetical order is: '+ result)
</code></pre>

<p>notice the change:</p>

<pre><code>  if alpha &lt; word:
     word = alpha
     sen = alpha 
</code></pre>

<p>you should save the first character of the new sequence</p>

python