Python Program to Sort Words in Alphabetic Order

In this program, you'll learn to sort the words in alphabetic order using for loop and display it.

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

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

Source Code

# Program to sort alphabetically the words form a string provided by the user

# change this value for a different result
my_str = "Hello this Is an Example With cased letters"

# uncomment to take input from the user
#my_str = input("Enter a string: ")

# breakdown the string into a list of words
words = my_str.split()

# sort the list
words.sort()

# display the sorted words

print("The sorted words are:")
for word in words:
   print(word)

Output

The sorted words are:
Example
Hello
Is
With
an
cased
letters
this

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.