DEV Community

Petros Demetrakopoulos
Petros Demetrakopoulos

Posted on

Quick Tip: Reversing a string in Python

You can easily reverse a string in python like this:

print("Hello"[::-1])
Enter fullscreen mode Exit fullscreen mode

This syntax reverses the string by converting it to an array (in python strings are actually arrays of characters) and then slicing it The syntax for the array slicing is as follows: [start:end:step] where empty start means 0 and empty stop means the length of the array. So [::-1] means to slice the array from the 1st element to the last element in reverse order.

Top comments (0)