Sort String Characters in Python

Sort the characters of a string in ascending order.

BeginnerString ProgramsExample 13 of 25
sort-string-characters.py
Run in browser
1# Program to sort characters of a string
2
3s = input("Enter a string: ")
4
5sorted_s = "".join(sorted(s))
6
7print("Sorted string:", sorted_s)

Output

Enter a string: dbca
Sorted string: abcd

What's going on

We use the built-in sorted() which returns a list of sorted characters, then join them back to a string.