Sort the words in lexicographical order in Python
Given a strings, we need to sort the words in lexicographical order (dictionary order).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input : "hello python program how are you" Output : are hello how program python you Input : "Coders loves the algorithms" Output : Coders algorithms loves the
Note: The words which have first letter is capital letter they will print according alphabetical manner.
Approach used in this program is very simple. Split the strings using split() function. After that sort the words in lexicographical order using sort(). Iterate the words through loop and print each word, which are already sorted.
alphabetical example hello how in is manner sort the this to word