How to Reverse a String in Python

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:

  • start and stop are omitted → Python considers the entire string
  • step = -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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top