Write a program in Python to sort all words of a sentence or string in dictionary (Alphabetical) order.
Input: Welcome to Pencil Programmer Official Website
Output: Official Pencil Programmer Website Welcome to
The method is very simple. We will first store the words into a python list and will use inbuilt sort() method on the newly created list to sort the words in alphabetical order.
At last, we will concatenate the words from start to end of the sorted list into a new Sentence and output it.
Let’s see its implementation in Python.
sentence = input("Enter a english sentence: ")
words = sentence.split()
newSentence = ""
for x in words:
newSentence += x + " "
Enter a english sentence: Welcome to Pencil Programmer Official Website
Official Pencil Programmer Website Welcome to
If you have any doubts or suggestions then comment below.