AppDividend
Latest Code Tutorials

How to Sort String in Python

0

Python offers a sort() function to perform the sorting. In Python, the string is immutable means once it is created, it can’t be changed. This is where things get a little complicated. The sort() function doesn’t work as it inplace tries to sort because you can not change a string once created. So let’s see how to sort a string alphabetically and reverse the string using the combination of different functions.

Python sort string

To sort a string alphabetically in Python, use the sorted() and string join() functions. The sorted() is a built-in Python function that returns a new sorted list from the items in iterable. The sorted() method returns a list of the characters of the string sorted alphabetically.

The string join() is a built-in Python function that returns the string concatenated with the elements of an iterable.

The task of sorting a string is performed in two steps. In the first step, we get the sorted list of characters, and then we join the result to get the resultant sorted string.

data = "immunoelectrophoretically"

print("The original string is : " + str(data))

sortedString = ''.join(sorted(data))

print("String after sorting : " + str(sortedString))

Output

The original string is : immunoelectrophoretically

String after sorting : acceeehiilllmmnoooprrttuy

You can see from the output that the combination of functions returns the alphabetically sorted string.

There is one more way to sort a string by using the combination of three functions.

Using sorted() + reduce() + lambda

The reduce() method is helpful when you have to apply a function to an iterable and reduce it to a single cumulative value. Python lambda function is the function that is defined without a name.

To sort a string alphabetically, use the combination of sorted(), reduce(), and lambda function.

Python does not have a built-in reduce() function. So, we need to import a package called functools, and that package has a reduce() function.

import functools

data = "immunoelectrophoretically"

print("The original string is : " + str(data))

sortedString = functools.reduce(lambda x, y: x + y, sorted(data))

print("String after sorting : " + str(sortedString))

Output

The original string is : immunoelectrophoretically

String after sorting : acceeehiilllmmnoooprrttuy

And we get the alphabetically sorted string in the output.

Python sort string in reverse order

To reverse a sorted string in Python, use the sorted() and join() function to pass the one more argument to the sorted() function, which is reverse = True, and it will return the sorted string in reverse order.

data = "immunoelectrophoretically"

print("The original string is : " + str(data))

reverseSorted = ''.join(sorted(data, reverse = True))

print("The reverse sorted string: ",reverseSorted)

Output

The original string is : immunoelectrophoretically

The reverse sorted string: yuttrrpooonmmllliiheeecca

In the output, you can see that it has returned a reverse sorted string.

Sort numeric strings in a list in Python

To sort numeric strings in a list, use the list.sort() function in which it has to be specified with the key as integer to convert it to integer while performing sort function internally.

import functools

list = ['46', '19', '11', '21', '29']

print("The original list is : " + str(list))

list.sort(key = int)

print("The sorted list is: ", list)

Output

The original list is : ['46', '19', '11', '21', '29']

The sorted list is:  ['11', '19', '21', '29', '46']

As you can see that it returns a list of strings sorted in numerically ascending order.

That is it for sorting a string in Python.

See also

Python sort set

Python sort list

Python sort dictionary by value

Python sort dictionary by key

Python ordered set

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.