Pure Python: Reversing a String

Except for programming examples, you almost never encounter the need to reverse a String. But if you would, how would you do it in Python? There are many ways, but there are two Pythonic ways, and depending on your taste, or if you have memory constraints, you might pick one over the other.

This is extended slice syntax. It works by doing [begin:end:step] – by leaving begin and end off and specifying a step of -1, it reverses a string.

When working with larger strings or when you just don’t want to reverse the whole string at once, you can use the reversed() built-in.