Sort String Characters in Python

Sort the characters of a string in ascending order.

BeginnerString ProgramsExample 13 of 25
sort-string-characters.py
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.

After this program

If the idea is still fuzzy, read Day 4: Strings and String Methods in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 string programs, or see every topic on Python exercises.