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
Answered by Andrei R. Python tutor

1543 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Make a program that asks the user for a series of numbers until they either want to output the average or quit the program.


Whats is the difference between a function and a procedure?


What is the difference between a list and an array?


Firstly, my question is not Python related, but maths A-level related. I don't know how to change that in my profile but I would like to only tutor maths GCSE and potentially A-level. My question: Integrate sin(x)^3 over x=0 and x=pi/2.


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences