Given string of words separated by some delimiter. The task is to sort all the words given in the string
Input : test_str = 'gfg:is:best:for:geeks', delim = "*" Output : best*for*geeks*gfg*is Explanation : Words sorted after separated by delim. Input : test_str = 'gfg:is:best', delim = "*" Output : best*gfg*is Explanation : Words sorted after separated by delim.
Method: Using sorted() + join() + split()
The combination of the above functions can be used to solve this problem. In this, we segregate all the words by a particular delimiter using split(), and convert to list, then perform the sort of words, then reconvert to string attaching by the same delimiter.
The original string is : gfg:is:best:for:geeks The sorted words : best:for:geeks:gfg:is
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python - Convert List to delimiter separated String
- Python - Convert Delimiter separated list to Number
- Python - Construct dictionary Key-Value pairs separated by delimiter
- Python - Custom Split Comma Separated Words
- Python | Pandas str.join() to join string/list elements with passed delimiter
- Python | Split on last occurrence of delimiter
- Python | Ways to split strings using newline delimiter
- Python - Replace delimiter
- Python - Convert delimiter seperated Mixed String to valid List
- Python - Segregate elements by delimiter
- Python - Concatenate Tuple elements by delimiter
- Python program to convert a list of strings with a delimiter to a list of tuple
- How to read a CSV file to a Dataframe with custom delimiter in Pandas?
- Python | Convert key-value pair comma separated string into dictionary
- Python | Convert list of strings to space separated string
- Python program to input a comma separated string
- Sort the words in lexicographical order in Python
- Python | Sort words of sentence in ascending order
- Python Program for Odd-Even Sort / Brick Sort
- Add elements in start to sort the array | Variation of Stalin Sort