Sort a given input of lowercase letters alphabetically

In order to sort the sequence alphabetically the student must keep in mind that each lowercase letter has a corresponding ASCII numerical code  defined such that its value increases linearly from A to Z. In Python, the function ord(character) returns the sought ASCII code.Knowing this, the problem just becomes a matter of sorting the letters from low to high value of the numerical ASCII code. A sample code is given below:

sequence=input("Enter your sequence:")

 for i in range(len(sequence)):

                for j in range(len(sequence)-1-i):

                        if ord(sequence[j])>ord(sequence[j+1]):

                                sequence[j],sequence[j+1]=sequence[j+1],sequence[j]

print sequence

AR

Related Python Mentoring answers

All answers ▸

Write a Python script to take a product name as input and then automatically google search reviews for it and open the top 3 search results in different tabs


What built-in types does python provide?


How would you get a piece of code to print the numbers 1 to 10


Write a simple number guessing game, give the user 3 tries to guess a number between 1 and 10