codescracker


python

Python Program to Sort String in Alphabetical Order



« Previous Program Next Program »


Sort String in Alphabetical Order in Python

To sort string in alphabetical order, you have to ask from user to enter any string to sort the entered string in alphabetical order and print the sorted string as output as shown in the program given below.

Python Programming Code to Sort String in Alphabetical Order

Following python program ask from the user to enter the string to sort it into alphabetical order to display the sorted string:

# Python Program - Sort String in Alphabetical Order

print("Enter 'x' for exit.");
string = input("Enter any string to sort in alphabetical order: ")
if string == 'x':
    exit();
else:
    print("\nString after sorted in alphabetical order:");
    print(''.join(sorted(string)));

Here is the sample run of the above python program to show how to sort string in alphabetical order:

sort string in alphabetical order python

Now enter any string say codescracker and press enter key to sort the entered string in alphabetical order, that is a comes first (if present) and z comes last (if present):

sort string alphabetical python

Same program on python shell:

python sort string in alphabetical order

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test