To reverse a string use the slice syntax:
my_string = "ferrari"
my_string_reversed = my_string[::-1]
print(my_string)
print(my_string_reversed)
ferrari
irarref
The slice syntax allows you to set a step, which is -1
in the example.
The default step is 1
, that is, go forward 1 character of the string at a time.
If you set the step to -1
you have the opposite, go back 1 character at a time.
So you start at the position of the last character and move backwards to the first character at position 0.
To learn more about slicing, check out Understanding Slicing in Python.