Hướng dẫn sort string python alphabetical
View Discussion Show
Improve Article Save Article View Discussion Improve Article Save Article Given a string of letters, write a python program to sort the given string in an alphabetical order. Example: Input : PYTHON Output : HNOPTY Input : Geeks Output : eeGks Naive Method to sort letters of string alphabeticallyHere we are converting the string into list and then finally sorting the entire list alphabet wise. Python3
Output: EEEEFGGKKORSS Using sorted() with join() to sort letters of string alphabeticallyHere we are converting the string into a sorted list and then finally joining them using join function. Python3
Output: HNOPTY Using sorted() with accumulate() to sort letters of string alphabeticallyHere we are importing accumulate from itertools module converting the string into a sorted list, and hence return the result Python3
Output: HNOPTY Using sorted() with reduce() to sort letters of string alphabeticallyAnother alternative is to use reduce() method. It applies a join function on the sorted list using ‘+’ operator. Python3
Output: HNOPTY When string is in different cases –Using sorted() with join() to sort letters of string alphabeticallyHere we are converting the string into a sorted list and then finally joining them using the lambda functions. Python3
Output: eeGks |