A program that prints the longest substring of s in which the letters occur in alphabetical order Assume s is a string of lower case characters.
Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print

Longest substring in alphabetical order is: beggh

In the case of ties, print the first substring. For example, if s = 'abcbcd', then your program should print

Longest substring in alphabetical order is: abc


I have been working on a edX course, MITx: 6.00.1x Introduction to Computer Science and Programming Using Python. Encountering the problem mentioned above, I set out to write a piece of code which I found to be the most disgusting to date(excluding Project Euler draft code).
Note: s can be any input, but in my case, edX will automatically input a value for s, so I do not need to define s.
first=0
second=1
strang1=''
strang2=''
while second&lt;len(s):
    if second&gt;=len(s):
        break
    elif s[first]&lt;=s[second]:
        strang1+=s[first:second+1]
        while second&lt;=len(s):
            second+=1
            if second&gt;=len(s):
                break
            elif s[second]&gt;=strang1[-1]:
                strang1+=s[second]
                if len(strang1)&gt;len(strang2):
                    strang2=strang1
            else:
                if len(strang1)&gt;len(strang2):
                    strang2=strang1
                strang1=''
                first=second-1
                break
    else:
        if len(s[first])&gt;len(strang2):
            strang2=s[first]
        first+=1
        second+=1
print(&quot;Longest substring in alphabetical order is:&quot; + strang2)

The code can accurately take on any input(if defined) and output it accurately(according to the edX's test runs). I'm looking to make my code more efficient, any suggestions? I'll accept any, fancy newfangled functions or maybe a total rework, I don't mind.
if second&gt;=len(s):
    break


This is not necessary. You have just checked that in the loop condition.



first=0
second=1
strang1=''
strang2=''


Consider renaming these variables to say what they do more clearly. Even "begin" and "end" would be clearer than "first" and second, but do not be afraid of longer variable names.

As a rule of thumb, be wary of variable names which you have numbered. They often mean either that they do sufficiently the same thing and you could simplify the code by putting them in a list and looping, or they do not do the same thing and you should name them appropriately. (As is the case here)



strang1+=s[second]
if len(strang1)&gt;len(strang2):


Constructing strings, especially one character at a time, is an expensive operation. You could do this more efficiently by just storing the location in the input string. You can compare sizes just by subtracting the start of that location from the end. At the end of the loop, you can assign the result in one go using s[start:end] notation.




  Assume s is a string of lower case characters.


When you have assumptions like this, it is generally prudent to check them. Coding challenges won't tend to require it. When you have a larger code base and are trying to work out where something went wrong, you'll thank your past self if the first thing that was given input it wasn't designed for is screaming about it so you know where to look.