{"id":10278,"date":"2021-10-01T10:00:52","date_gmt":"2021-10-01T04:30:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=10278"},"modified":"2021-11-22T18:33:29","modified_gmt":"2021-11-22T13:03:29","slug":"python-program-to-accept-a-hyphen-separated-sequence-of-words-as-input-and-print-the-words-in-a-hyphen-separated-sequence-after-sorting-them-alphabetically","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-accept-a-hyphen-separated-sequence-of-words-as-input-and-print-the-words-in-a-hyphen-separated-sequence-after-sorting-them-alphabetically\/","title":{"rendered":"Python Program to Accept a Hyphen Separated Sequence of Words as Input and Print the Words in a Hyphen-Separated Sequence after Sorting them Alphabetically"},"content":{"rendered":"
Strings in Python:<\/strong><\/p>\n “String is a character collection or array”<\/strong><\/p>\n Well in Python too, for the string data type, we say the same definition. The string is a sequenced character array and is written within single, double, or three quotes. Also, Python does not have the data type character, thus it is used as a string of length 1 if we write ‘s’.<\/p>\n Given a hyphen-separated sequence of strings, the task is to sort the strings and print them as hyphen-separated strings 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 Examples3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to accept a hyphen-separated sequence of strings, the task is to sort the strings and print them as hyphen-separated strings in Python.<\/p>\n Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs<\/a> examples and excel in coding the complex logic.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation of the above approach:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation of the above approach:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Strings in Python: “String is a character collection or array” Well in Python too, for the string data type, we say the same definition. The string is a sequenced character array and is written within single, double, or three quotes. Also, Python does not have the data type character, thus it is used as a …<\/p>\ngiven hyphen-separated string =hello-this-is-btechgeeks<\/pre>\n
The string before modification = hello-this-is-btechgeeks\r\nThe string after modification = btechgeeks-hello-is-this<\/pre>\n
given hyphen-separated string =good-morning-codechef<\/pre>\n
The string before modification = good-morning-codechef\r\nThe string after modification = codechef-good-morning<\/pre>\n
given hyphen-separated string =btechgeeks-online-platform-fror-coding-students<\/pre>\n
The string before modification = btechgeeks-online-platform-fror-coding-students\r\nThe string after modification = btechgeeks-coding-fror-online-platform-students<\/pre>\n
Program to Accept a Hyphen Separated Sequence of Words as Input and Print the Words in a Hyphen-Separated Sequence after Sorting them Alphabetically in Python<\/h2>\n
\n
Method #1:Using split() and join() functions(Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngivn_strng = 'hello-this-is-btechgeeks'\r\n# print the string before modification\r\nprint('The string before modification = ', givn_strng)\r\n# Split the hyphen-separated strings into a list of strings using the split()\r\n# function and store it in a variable.\r\nwordsLis = givn_strng.split('-')\r\n# sort the given list using the sort() function.\r\nwordsLis.sort()\r\n# Print the sorted sequence by joining the words in the list with a hyphen.\r\nresultwords = '-'.join(wordsLis)\r\n# print the resultwords\r\nprint('The string after modification = ', resultwords)\r\n<\/pre>\n
The string before modification = hello-this-is-btechgeeks\r\nThe string after modification = btechgeeks-hello-is-this<\/pre>\n
\n
Method #2:Using split() and join() functions(User Input)<\/h3>\n
\n
# Give the hyphen-separated string as user input using the input() function.\r\ngivn_strng = input('Enter some random string = ')\r\n# print the string before modification\r\nprint('The string before modification = ', givn_strng)\r\n# Split the hyphen-separated strings into a list of strings using the split()\r\n# function and store it in a variable.\r\nwordsLis = givn_strng.split('-')\r\n# sort the given list using the sort() function.\r\nwordsLis.sort()\r\n# Print the sorted sequence by joining the words in the list with a hyphen.\r\nresultwords = '-'.join(wordsLis)\r\n# print the resultwords\r\nprint('The string after modification = ', resultwords)\r\n<\/pre>\n
Enter some random string = good-morning-codechef\r\nThe string before modification = good-morning-codechef\r\nThe string after modification = codechef-good-morning<\/pre>\n
\n
\n