List Slicing


#1

garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
message = garbled[::-1]
print message[::2]

help:
I've got the secret message in the console even i still getting error!


#2

Thanks any way I solved by trying again and again myself!

the correct answer is:

garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
message = garbled[::-2]
print message


#3

The slicing exercise works like this:

print garbled[start:end:stride]

start: the first character to include
end: the first character to NOT include (i.e. include everything before this point)
stride: the number of characters to step.

If you're going forwards, the character positions are positive, with 0 being the first character ("!").
If you're going backwards, the character positions are negative, with -1 being the last character ("I")
The first character to NOT include is the (length of the string +1) - i.e. INCLUDE the last character.
Again, as we're counting backwards, this is negative.
so

    garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
    gblen=len(garbled)+1
    print garbled[-1:-gblen:-2]

gives us

    I am the secret message!

In Python shorthand, this is the same as:

    print garbled[::-2]

because python assumes that, as you're stepping backwards and you don't provide values, then "start" is the end of the string and "end" needs to include the beginning of the string.
Hope that helps!