{"id":14018,"date":"2021-10-01T10:00:57","date_gmt":"2021-10-01T04:30:57","guid":{"rendered":"https:\/\/python-programs.com\/?p=14018"},"modified":"2021-11-22T18:33:29","modified_gmt":"2021-11-22T13:03:29","slug":"python-program-to-rearrange-the-letters-of-a-string-in-alphabetical-order","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-rearrange-the-letters-of-a-string-in-alphabetical-order\/","title":{"rendered":"Python Program to Rearrange the Letters of a String in Alphabetical Order"},"content":{"rendered":"
Given a string, the task is to rearrange the letters of a string in Alphabetical order in Python.<\/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 rearrange the letters of a string in Alphabetical order in Python.<\/p>\n Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java<\/a> and get a good grip on it for better results in exams or interviews.<\/p>\n i)Single String<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii)Multiple strings separated by spaces<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n i)Single String<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii)Multiple strings separated by spaces<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n This procedure is similar to the last one. The only difference is that we use a lambda expression with the reduce() function to connect the strings here.<\/p>\n i)Single String<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii)Multiple strings separated by spaces<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n This procedure is similar to the last one. The only difference is that we use a lambda expression with the reduce() function to connect the strings here.<\/p>\n i)Single String<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii)Multiple strings separated by spaces<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Given a string, the task is to rearrange the letters of a string in Alphabetical order in Python. Examples: Example1: Input: Given string = btechgeeks Output: The original string is [ btechgeeks ] The modified string after reordering the letters of a string alphabetically is [ bceeeghkst ] Example2: Input: Given string =Good morning this …<\/p>\nGiven string = btechgeeks<\/pre>\n
The original string is [ btechgeeks ]\r\nThe modified string after reordering the letters of a string alphabetically is [ bceeeghkst ]<\/pre>\n
Given string =Good morning this is BTechgeeks<\/pre>\n
Enter some random string = Good morning this is BTechgeeks\r\nThe original string is [ Good morning this is BTechgeeks ]\r\nThe modified string after reordering the letters of a string alphabetically is [ BGTcdeeegghhiiikmnnooorssst ]<\/pre>\n
Program to Rearrange the Letters of a String in Alphabetical Order in Python<\/h2>\n
\n
Method #1: Using sorted() and join() functions (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in variable.\r\ngvnstrng = 'btechgeeks'\r\n# Reorder the letters of a string alphabetically using\r\n# the sorted function(This method returns a list of letters in alphabetical order).\r\nsortdstrng = sorted(gvnstrng)\r\n# Join this using the join() function.\r\nfinalstrng = ''.join(sortdstrng)\r\n# Print the modified string after reordering the letters of a string alphabetically.\r\nprint('The original string is [', gvnstrng, ']')\r\nprint(\r\n 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')\r\n<\/pre>\n
The original string is [ btechgeeks ]\r\nThe modified string after reordering the letters of a string alphabetically is [ bceeeghkst ]<\/pre>\n
\n
# Give the string as static input and store it in variable.\r\ngvnstrng = 'Hello this is BTechgeeks'\r\n# Reorder the letters of a string alphabetically using\r\n# the sorted function(This method returns a list of letters in alphabetical order).\r\nsortdstrng = sorted(gvnstrng)\r\n# Join this using the join() function.\r\n# Use the strip() function to remove spaces between the strings.\r\nfinalstrng = ''.join(sortdstrng).strip()\r\n# Print the modified string after reordering the letters of a string alphabetically.\r\nprint('The original string is [', gvnstrng, ']')\r\nprint(\r\n 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')\r\n<\/pre>\n
The original string is [ Hello this is BTechgeeks ]\r\nThe modified string after reordering the letters of a string alphabetically is [ BHTceeeeghhiikllossst ]<\/pre>\n
Method #2: Using sorted() and join() functions (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvnstrng = input('Enter some random string = ')\r\n# Reorder the letters of a string alphabetically using\r\n# the sorted function(This method returns a list of letters in alphabetical order).\r\nsortdstrng = sorted(gvnstrng)\r\n# Join this using the join() function.\r\nfinalstrng = ''.join(sortdstrng)\r\n# Print the modified string after reordering the letters of a string alphabetically.\r\nprint('The original string is [', gvnstrng, ']')\r\nprint(\r\n 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')\r\n<\/pre>\n
Enter some random string = btechgeeks\r\nThe original string is [ btechgeeks ]\r\nThe modified string after reordering the letters of a string alphabetically is [ bceeeghkst ]<\/pre>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvnstrng = input('Enter some random string = ')\r\n# Reorder the letters of a string alphabetically using\r\n# the sorted function(This method returns a list of letters in alphabetical order).\r\nsortdstrng = sorted(gvnstrng)\r\n# Join this using the join() function.\r\n# Use the strip() function to remove spaces between the strings.\r\nfinalstrng = ''.join(sortdstrng).strip()\r\n# Print the modified string after reordering the letters of a string alphabetically.\r\nprint('The original string is [', gvnstrng, ']')\r\nprint(\r\n 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')\r\n<\/pre>\n
Enter some random string = Good morning this is BTechgeeks\r\nThe original string is [ Good morning this is BTechgeeks ]\r\nThe modified string after reordering the letters of a string alphabetically is [ BGTcdeeegghhiiikmnnooorssst ]<\/pre>\n
Method #3: Using sorted() and lambda expression (Static Input)<\/h3>\n
\n
# import the reduce from functools using the import Keyword\r\nfrom functools import reduce\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngvnstrng = 'btechgeeks'\r\n# Use the reduce and sorted() function in a lambda expression and store it in a variable.\r\nfinalstrng = reduce(lambda m, n: m + n, (sorted(gvnstrng)))\r\n# Print the modified string after reordering the letters of a string alphabetically.\r\nprint('The original string is [', gvnstrng, ']')\r\nprint(\r\n 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')\r\n<\/pre>\n
The original string is [ btechgeeks ]\r\nThe modified string after reordering the letters of a string alphabetically is [ bceeeghkst ]<\/pre>\n
\n
# import the reduce from functools using the import Keyword\r\nfrom functools import reduce\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngvnstrng = 'this is BTechgeeks'\r\n# Use the reduce and sorted() function in a lambda expression and store it in a variable.\r\n# Use the strip() function to remove spaces between the strings.\r\nfinalstrng = reduce(lambda m, n: m + n, (sorted(gvnstrng))).strip()\r\n# Print the modified string after reordering the letters of a string alphabetically.\r\nprint('The original string is [', gvnstrng, ']')\r\nprint(\r\n 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')\r\n<\/pre>\n
The original string is [ this is BTechgeeks ]\r\nThe modified string after reordering the letters of a string alphabetically is [ BTceeeghhiikssst ]<\/pre>\n
Method #4: Using sorted() and lambda expression (User Input)<\/h3>\n
\n
# import the reduce from functools using the import Keyword\r\nfrom functools import reduce\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngvnstrng = input('Enter some random string = ')\r\n# Use the reduce and sorted() function in a lambda expression and store it in a variable.\r\nfinalstrng = reduce(lambda m, n: m + n, (sorted(gvnstrng)))\r\n# Print the modified string after reordering the letters of a string alphabetically.\r\nprint('The original string is [', gvnstrng, ']')\r\nprint(\r\n 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')\r\n<\/pre>\n
The original string is [ btechgeeks ]\r\nThe modified string after reordering the letters of a string alphabetically is [ bceeeghkst ]<\/pre>\n
\n
# import the reduce from functools using the import Keyword\r\nfrom functools import reduce\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngvnstrng = input('Enter some random string = ')\r\n# Use the reduce and sorted() function in a lambda expression and store it in a variable.\r\n# Use the strip() function to remove spaces between the strings.\r\nfinalstrng = reduce(lambda m, n: m + n, (sorted(gvnstrng))).strip()\r\n# Print the modified string after reordering the letters of a string alphabetically.\r\nprint('The original string is [', gvnstrng, ']')\r\nprint(\r\n 'The modified string after reordering the letters of a string alphabetically is [', finalstrng, ']')\r\n<\/pre>\n
Enter some random string = Good morning this is BTechgeeks\r\nThe original string is [ Good morning this is BTechgeeks ]\r\nThe modified string after reordering the letters of a string alphabetically is [ BGTcdeeegghhiiikmnnooorssst ]<\/pre>\n
\n