English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced Python knowledge

Python reference manual

Python program sorts words in alphabetical order

Complete Python Examples

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 explain how to sort words in alphabetical order (dictionary order).

Source code

# 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 the user
#my_str = input("Enter a string: ")
# Split string into word list
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 to a list of words. The split() method splits the string into spaces.

Then usesort() methodSort the word list and display all words.

Complete Python Examples