Reverse Order of Words

Reverse the order of words in string.

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

# Reverse words
words = s.split()
reversed_words = words[::-1]
result = " ".join(reversed_words)

print(f"Reversed words: {result}")

Output

Enter a string: Hello World Programming
Reversed words: Programming World Hello

Split, reverse list, join back.

Key Concepts:

  • Split into words
  • Reverse list using [::-1]
  • Join with space