Python Program to Sort Words in Alphabetic Order
Sorting refers to the process of arranging a collection of items in a specific order or sequence according to some criterion. The criterion can be any property or characteristic of the items being sorted, such as their numerical value, alphabetical order, size, color, or some other attribute.
Python Code :
The below Python program sorts words in alphabetical order:
# Take input string from user
string = input("Enter a string: ")
# Split the string into words
words = string.split()
# Sort the words in alphabetical order
words.sort()
# Display the sorted words
print("Sorted words:", " ".join(words))
Explanation:
The program takes a string input from the user and stores it in the variable string.
The split() method is used to split the string into words and store them in a list called words.
The sort() method is used to sort the words in alphabetical order.
The join() method is used to join the sorted words back into a string, separated by spaces. The resulting string is displayed using the print() function.
For Example:
Enter a string: Elon Musk is Twitter CEO
Sorted words: CEO Elon Musk Twitter is