English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this program, you will learn to use the for loop to sort words in alphabetical order and display them.
To understand this example, you should know the followingPython programmingTopic:
In this example, we demonstrate how to sort words in alphabetical order (dictionary order) and display them.
# The program sorts the words provided by the user in alphabetical order my_str = "Hello this Is an Example With cased letters" # Get input from user #my_str = input("Enter a string:") # Split string into list of words words = my_str.split() # List sorting words.sort() # Display sorted words print("The sorted words are:") for word in words: print(word)
Output result
The sorted words are: Example Hello Is With an cased letters this
Note:To test the program, please change the value of my_str.
In this program, we will store the string to be sorted in my_str. Use the split() method to convert the string into a list of words. The split() method splits the string into whitespace.
Then usesort() methodSort the word list and display all words.