Reverse a string or list in Python with the [::-1] slice

Want to reverse a sequence (string/list/tuple)? Use a 3-argument slice:

s[::-1]

This works because:

– Empty start means “from the start”
– Empty end means “through the end”
– Step size of -1 means “go back 1 each time”

This returns a new value, from s’s end to its start.