{"id":17568,"date":"2021-08-26T10:42:24","date_gmt":"2021-08-26T05:12:24","guid":{"rendered":"https:\/\/python-programs.com\/?p=17568"},"modified":"2021-11-22T18:37:12","modified_gmt":"2021-11-22T13:07:12","slug":"python-program-to-sort-words-in-alphabetic-order","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-sort-words-in-alphabetic-order\/","title":{"rendered":"Python Program to Sort words in Alphabetic Order"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Longest Word from Sentence<\/a> sort() method :<\/strong><\/p>\n By default, the sort() method sorts the list in ascending order.<\/p>\n split() method:<\/strong><\/p>\n The split() method in Python divides a string into a list of strings by breaking the string with the specified separator.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to sort all words in a given sentence in alphabetic order.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Find Longest Word from Sentence Given a string and the task is to sort all words in a given sentence in alphabetic order. sort() method : By default, the sort() method sorts the list in ascending order. split() method: The split() method in Python divides …<\/p>\n
\nGiven a string and the task is to sort all words in a given sentence in alphabetic order.<\/p>\nGiven String = \"Hello this is btechgeeks\"<\/pre>\n
The all sorted words of given sentence in Alphabetic order :\r\nHello\r\nbtechgeeks\r\nis\r\nthis<\/pre>\n
Given String = \"Good morning this is btechgeeks\"<\/pre>\n
The all sorted words of given sentence in Alphabetic order :\r\nGood\r\nbtechgeeks\r\nis\r\nmorning\r\nthis<\/pre>\n
Program to Sort words in Alphabetic Order<\/h2>\n
\n
Method #1: Using sort() Method (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngven_str = \"Hello this is btechgeeks\"\r\n# Split the given string into list of words using the split() function and store\r\n# it in another variable say \"wrd_lst\".\r\nwrd_lst = gven_str.split()\r\n# Sort the above obtained word list using the sort() method.\r\nwrd_lst.sort()\r\n# Loop in the above list \"wrd_lst\" using the for loop.\r\nprint(\"The all sorted words of given sentence in Alphabetic order :\")\r\nfor wrd in wrd_lst:\r\n # Print the iterator value to sort all words in a given sentence in alphabetic order.\r\n print(wrd)\r\n<\/pre>\n
The all sorted words of given sentence in Alphabetic order :\r\nHello\r\nbtechgeeks\r\nis\r\nthis<\/pre>\n
Method #2: Using sort() Method (User Input)<\/h3>\n
\n
# Give the string as user input using input() function and store it in a variable.\r\ngven_str = input(\"Enter some random string = \")\r\n# Split the given string into list of words using the split() function and store\r\n# it in another variable say \"wrd_lst\".\r\nwrd_lst = gven_str.split()\r\n# Sort the above obtained word list using the sort() method.\r\nwrd_lst.sort()\r\n# Loop in the above list \"wrd_lst\" using the for loop.\r\nprint(\"The all sorted words of given sentence in Alphabetic order :\")\r\nfor wrd in wrd_lst:\r\n # Print the iterator value to sort all words in a given sentence in alphabetic order.\r\n print(wrd)\r\n<\/pre>\n
Enter some random string = Good morning this is btechgeeks\r\nThe all sorted words of given sentence in Alphabetic order :\r\nGood\r\nbtechgeeks\r\nis\r\nmorning\r\nthis<\/pre>\n
\n