Sort Words Alphabetically

Sort words in alphabetical order.

Logic BuildingIntermediate
Logic Building
# Take string input
s = input("Enter a string: ")

# Sort words
words = s.split()
sorted_words = sorted(words)
result = " ".join(sorted_words)

print(f"Sorted: {result}")

Output

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

Sort word list and join back.

Key Concepts:

  • Split into words
  • Sort using sorted()
  • Join with space