Python Program to Sort Words in Alphabetic Order Ad Sponsored

ⓘ Sponsored by

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

# Display the sorted words
print("Sorted words:", " ".join(words))


  1. The program takes a string input from the user and stores it in the variable string.

  2. The split() method is used to split the string into words and store them in a list called words.

  3. The sort() method is used to sort the words in alphabetical order.

  4. 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 Ad Sponsored

ⓘ Sponsored by