List Slicing

Hey guys! I’d really appreciate it if someone can help me out here by telling me if there’s any other way I could’ve written the code.

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

Here they wanted me to reverse it and choose every other letter.

The way you did it is perfectly fine.
It’s also reasonably clear what is happening to any python programmer
you could do something clever like print garbled[::-1][::2]
or

decoded = garbled[::2][::-1] 
print decoded

Doesn’t matter, point is you understand list slicing in python.
Write it as a function for practice and pat yourself on the back

1 Like