This post will discuss how to sort a string in Python.

Since strings are immutable in Python, you cannot simply shuffle characters in a string to convert it in sorted order. However, you can create a copy of the string in sorted order. This post provides an overview of possible ways to accomplish this in Python.

1. Using sorted() function

A simple solution is to use the built-in function sorted(). Since it returns a new sorted list of characters, you can construct a string using the str.join() function. This would translate to a simple code below:

Download  Run Code

2. Using reduce operation

Another option is to perform a reduction operation using the functools.reduce function. Here’s an example of its usage:

Download  Run Code

That’s all about sorting a string in Python.