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 string of length 1 if we write ‘s’.
Given a hyphen-separated sequence of strings, the task is to sort the strings and print them as hyphen-separated strings in Python.
Examples:
Example1:
Input:
given hyphen-separated string =hello-this-is-btechgeeks
Output:
The string before modification = hello-this-is-btechgeeks The string after modification = btechgeeks-hello-is-this
Example2:
Input:
given hyphen-separated string =good-morning-codechef
Output:
The string before modification = good-morning-codechef The string after modification = codechef-good-morning
Examples3:
Input:
given hyphen-separated string =btechgeeks-online-platform-fror-coding-students
Output:
The string before modification = btechgeeks-online-platform-fror-coding-students The string after modification = btechgeeks-coding-fror-online-platform-students
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
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.
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Method #1:Using split() and join() functions(Static Input)
Approach:
- Give the hyphen-separated string as static input and store it in a variable.
- Split the hyphen-separated strings into a list of strings using the split() function and store it in a variable.
- sort the given list using the sort() function.
- Print the sorted sequence by joining the words in the list with a hyphen.
- The Exit of the program.
Below is the implementation of the above approach:
# Give the string as static input and store it in a variable. givn_strng = 'hello-this-is-btechgeeks' # print the string before modification print('The string before modification = ', givn_strng) # Split the hyphen-separated strings into a list of strings using the split() # function and store it in a variable. wordsLis = givn_strng.split('-') # sort the given list using the sort() function. wordsLis.sort() # Print the sorted sequence by joining the words in the list with a hyphen. resultwords = '-'.join(wordsLis) # print the resultwords print('The string after modification = ', resultwords)
Output:
The string before modification = hello-this-is-btechgeeks The string after modification = btechgeeks-hello-is-this
Explanation:
- Give the hyphen-separated string as static input and store it in a variable.
- The hyphen is used as a key to split the sequence, and the words are saved in a list.
- Using the sort() function, the words in the list are sorted alphabetically.
- The terms in the list are then connected together by utilizing a hyphen as a reference.
- The word sequence is then printed in its sorted order.
Method #2:Using split() and join() functions(User Input)
Approach:
- Give the hyphen-separated string as user input using the input() function.
- Split the hyphen-separated strings into a list of strings using the split() function and store it in a variable.
- sort the given list using the sort() function.
- Print the sorted sequence by joining the words in the list with a hyphen.
- The Exit of the program.
Below is the implementation of the above approach:
# Give the hyphen-separated string as user input using the input() function. givn_strng = input('Enter some random string = ') # print the string before modification print('The string before modification = ', givn_strng) # Split the hyphen-separated strings into a list of strings using the split() # function and store it in a variable. wordsLis = givn_strng.split('-') # sort the given list using the sort() function. wordsLis.sort() # Print the sorted sequence by joining the words in the list with a hyphen. resultwords = '-'.join(wordsLis) # print the resultwords print('The string after modification = ', resultwords)
Output:
Enter some random string = good-morning-codechef The string before modification = good-morning-codechef The string after modification = codechef-good-morning
Explanation:
- As input, the user must enter a hyphen-separated string of words.
- The hyphen is used as a key to split the sequence, and the words are saved in a list.
- Using the sort() function, the words in the list are sorted alphabetically.
- The terms in the list are then connected together by utilizing a hyphen as a reference.
- The word sequence is then printed in its sorted order.
- It is the fastest and efficient approach.
Related Programs:
- Python Program to Read a List of Words and Return the Length of the Longest Word
- Python Program to Create a Dictionary with Key as First Character and Value as Words Starting with that Character
- Python Program to Count the Frequency of Words Appearing in a String Using a Dictionary
- Python Program to Read a File and Capitalize the First Letter of Every Word in the File
- Python Program to Count the Number of Words in a Text File