One of the ways one can reverse a string in python is like so:
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> mystring = "Hello, World!" >>> mystring[::-1] '!dlroW ,olleH' >>>
It is my understanding that here, the first argument of the slice operator defaults to the beginning of the string. The second argument defaults to the end of the string (if no argument is given). Finally, the "third argument", added relatively recently, reverses the operation when "-1" is passed.
Thus, the string is printed in reverse.
However, if the above is true, why does this happen?
>>> mystring[0:13] 'Hello, World!' #yup, that's fine >>> mystring[0:13:-1] '' #hmm, what?
It seems that explicitly stating the range messes up the third argument. Why? Why does it print an empty string?