Ad
Fundamentals
Strings

The most elegant way to do this is through slicing.

The first two positions of the indexing represent the starting and ending positions of the cursor. Because they are empty, it is implicit we are traversing the whole string.

The last position of the indexing, "-1", is the step at which the cursor moves through the string.

So the cursor moves one position at a time from the last char to the first

Code
Diff
  • def reverse_string(string):
        return string[::-1]
    
    • def reverse_string(string):
    • reversed_list = [string[letter] for letter in range(len(string) - 1, -1, -1)]
    • return "".join(reversed_list)
    • return string[::-1]