Python Program to Sort Words in Alphabetic Order

To understand this example, you should have the knowledge of the following Python programming topics:


In this example, we illustrate how words can be sorted lexicographically (alphabetic order).

Source Code

Output

The sorted words are:
an
cased
example
hello
is
letters
this
with

Note: To test the program, change the value of my_str.

In this program, we store the string to be sorted in my_str. Using the split() method the string is converted into a list of words. The split() method splits the string at whitespaces.

The list of words is then sorted using the sort() method, and all the words are displayed.