The easiest way to reverse a string in Python is by using the slicing method:
string = "xyz"
reversed_string = string[::-1]
print(reversed_string)
Explanation:
string = "xyz"
A variable is assigned the value "xyz".
string[::-1]
This is a slicing operation with the format:string[start:stop:step]
Here:
startandstopare omitted → Python considers the entire stringstep = -1→ moves backward through the string
So, it reads the string from end to beginning, producing "zyx".
Key Insight:
Using [::-1] is one of the most efficient and concise ways to reverse a string in Python without writing loops or using extra functions.