Sort Words Alphabetically

Sort words in alphabetical order.

IntermediatePhase 5 - Category 3: Word OperationsExample 10 of 10
sort-words-alphabetically.py
1# Take string input
2s = input("Enter a string: ")
3
4# Sort words
5words = s.split()
6sorted_words = sorted(words)
7result = " ".join(sorted_words)
8
9print(f"Sorted: {result}")

Output

Enter a string: world hello programming
Sorted: hello programming world

What's going on

Sort word list and join back.

Key Concepts:

Split into words
Sort using sorted()
Join with space