Reverse a string

This is one of those things that sounds quite simple, but seems to generate quite a lot of discussion on the best way to do it. If you're interested in diving into that discussion, take a look at this StackOverflow question and the answers. 

If, however, all you care about is actually reversing a string with Python, here's a couple of ways to do it. 

Let's say we have a string:

mystring = 'Hello my name is Daniel

Method 1

This method uses reverse() and has the benefit of being comparatively readable in comparison to Method 2.

print (''.join(reversed(mystring)))


leinaD si eman ym olleH

Method 2

This method uses extended slice, which essentially slices one character of the end of the string in successive steps. Not as obvious as method 1, if you ask me. 

print (mystring[::-1])


leinaD si eman ym olleH