Sort Words in Alphabetical Order in Python

Python Script to Sort Words in Alphabetic Order from a Sentence. It will be very useful when you are dealing with text processing systems.

Source code :

string = "I like going out to parties with friends or watching TV"

#split the words
words = string.split()

#sort them

#join and print them
print("Sorted sentence: ", " ".join(words))

Output :

Sorted sentence:  I TV friends going like or out parties to watching with

Notes :

In this script, we have assigned a sentence to the string variable, then splitted the sentence using the split() function in python and store it to the variable words, Then by calling the sort() function the words list will be sorted.

Finally we have joined the list using " ".join(words) function and printed the sentence which is sorted in Alphabetical Order

Output :


Comments :