Python Example | Sort a Python String

In python, we can use sorted() function to sort a python string. This example illustrates how to do.

For example:

org_str = 'cebad'

new_str_list = sorted(org_str)
# cebad
# ['a', 'b', 'c', 'd', 'e']

This example code sorts org_str using sorted() function and return a new python list new_str_list.

In order to get a python string, we can use join() function to convert a python list to string.

Here is an example:

new_str = ''.join(new_str_list)
# abcde

We also can sort a string reversely. Here is an example:

new_str_reverse = ''.join(sorted(org_str, reverse=True))
# edcba