How to Reverse a String

Reversing a string in Python can be done using slicing. A simple and efficient way is to use the slice notation [::-1].

Example: Reversing a String

# Reversing a string
text = "Hello, World!"
reversed_text = text[::-1]
print(reversed_text)

Output

!dlroW ,olleH

Explanation: The slice notation [::-1] reverses the string by starting from the end and stepping backward.