Python | Ways to sort letters of string alphabetically Last Updated : 25 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 : eeGksNaive Method to sort letters of string alphabetically Here we are converting the string into list and then finally sorting the entire list alphabet wise. Python3 s ="GEEKSFORGEEKS" li = [] l = len(s) for i in range (0,l): li.append(s[i]) for i in range(0,l): for j in range(0,l): if li[i]<li[j]: li[i],li[j]=li[j],li[i] j="" for i in range(0,l): j = j+li[i] print(j) Output: EEEEFGGKKORSSUsing sorted() with join() to sort letters of string alphabetically Here we are converting the string into a sorted list and then finally joining them using join function. Python3 # Python3 program to sort letters # of string alphabetically def sortString(str): return ''.join(sorted(str)) # Driver code str = 'PYTHON' print(sortString(str)) Output: HNOPTYUsing sorted() with accumulate() to sort letters of string alphabetically Here we are importing accumulate from itertools module converting the string into a sorted list, and hence return the result Python3 # Python3 program to sort letters # of string alphabetically from itertools import accumulate def sortString(str): return tuple(accumulate(sorted(str)))[-1] # Driver code str = 'PYTHON' print(sortString(str)) Output: HNOPTYUsing sorted() with reduce() to sort letters of string alphabetically Another alternative is to use reduce() method. It applies a join function on the sorted list using '+' operator. Python3 # Python3 program to sort letters # of string alphabetically from functools import reduce def sortString(str): return reduce(lambda a, b : a + b, sorted(str)) # Driver code str = 'PYTHON' print(sortString(str)) Output: HNOPTYWhen string is in different cases -Using sorted() with join() to sort letters of string alphabetically Here we are converting the string into a sorted list and then finally joining them using the lambda functions. Python3 # Python3 program to sort letters # of string alphabetically from itertools import accumulate def sortString(str): return "".join(sorted(str, key = lambda x:x.lower())) # Driver code str = 'Geeks' print(sortString(str)) Output: eeGks Time Complexity: O(n*logn), as sorted() function is used.Auxiliary Space: O(n), where n is length of string. Comment More infoAdvertise with us Next Article Python Program to Sort Words in Alphabetical Order S Smitha Dinesh Semwal Follow Improve Article Tags : Python Python Programs Python-string-functions Python-sort Practice Tags : python Similar Reads How To Sort List Of Strings In Alphabetically When working with Python programs, be it any machine learning program or simple text processing program, you might have come across a need to sort a list of strings alphabetically, either in ascending or reverse order. Strings are sorted alphabetically based on their initial letter (a-z or A-Z). But 3 min read Python program to sort a list of tuples alphabetically Given a list of tuples, write a Python program to sort the tuples alphabetically by the first item of each tuple. Examples: Input: [("Amana", 28), ("Zenat", 30), ("Abhishek", 29), ("Nikhil", 21), ("B", "C")] Output: [('Amana', 28), ('Abhishek', 29), ('B', 'C'), ('Nikhil', 21), ('Zenat', 30)] Input: 3 min read Python Program to Sort Words in Alphabetical Order The task is to write a program that takes a list of words as input and sorts them in alphabetical order. The program should display the sorted list of words, ensuring that the sorting is case-insensitive. To sort words in alphabetical order in Python, we can use the sorted() function or the sort() m 2 min read How to sort a list of strings in Python In this article, we will explore various methods to sort a list of strings in Python. The simplest approach is by using sort().Using sort() MethodThe sort() method sorts a list in place and modifying the original list directly.Pythona = ["banana", "apple", "cherry"] # Sorting list in place a.sort() 2 min read How to Sort a List Alphabetically in Python? Sorting lists alphabetically is a common task in Python, whether we're organizing names, cleaning data or building an interface. Python makes sorting easy with several built-in methods and libraries for both simple and advanced sorting needs.Let's look at some of the most common methods one by one:U 2 min read Python - Differential Sort String Numbers and Alphabets Given a List String, Reorder List, with Sorted Alphabets followed by Sorted Strings. Input : test_list = ["1", "G", "10", "L", "9", "K", "4"] Output : ['G', 'K', 'L', '1', '4', '9', '10'] Explanation : Alphabets sorted, succeeded by sorted digits. Input : test_list = ["1", "G", "10", "L", "9"] Outpu 5 min read Like