Reverse Each Word

Reverse each word in string.

IntermediatePhase 5 - Category 4: String TransformationExample 10 of 10
reverse-each-word.py
1# Take string input
2s = input("Enter a string: ")
3
4# Reverse each word
5words = s.split()
6reversed_words = [word[::-1] for word in words]
7result = " ".join(reversed_words)
8
9print(f"Result: {result}")

Output

Enter a string: Hello World
Result: olleH dlroW

What's going on

Reverse each word individually.

Key Concepts:

Split into words
Reverse each word using [::-1]
Join back with space