Reverse Order of Words

Reverse the order of words in string.

IntermediatePhase 5 - Category 3: Word OperationsExample 3 of 10
reverse-order-of-words.py
1# Take string input
2s = input("Enter a string: ")
3
4# Reverse words
5words = s.split()
6reversed_words = words[::-1]
7result = " ".join(reversed_words)
8
9print(f"Reversed words: {result}")

Output

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

What's going on

Split, reverse list, join back.

Key Concepts:

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