Python Program - Sort Words in Alphabetic Order
Here's a Python program to sort words in alphabetical order:
t refero:theitroad.com# take input from the user words = input("Enter words separated by space: ") # split the words into a list words_list = words.split() # sort the words sorted_words = sorted(words_list) # display the sorted words print("The sorted words are:") for word in sorted_words: print(word)
Output:
Enter words separated by space: banana apple orange grape The sorted words are: apple banana grape orange
In this program, we take a string of words as input from the user using the input()
function. We then split this input string into a list of words using the split()
function.
We sort the words in alphabetical order using the sorted()
function and store the result in a variable called sorted_words
.
Finally, we display the sorted words using a for
loop and the print()
function.