Python Slicing and Reversing — Visual

Pictures help when trying to understand slicing, especially when using negative indexes along with positive indexes.

String sample

String sample for the examples

1. str[:0:-1]

str[:0:-1]

2. str[:3:-1]

str[:3:-1]

3. str[:2:-1]

str[:2:-1]

4. str[-2:-6:-1]

str[-2:-6:-1]

5. str[-4:-1:-1]

str[-4:-1:-1]

6. str[-4:1:-1]

str[-4:1:-1]

7. str[-3:1:-1]

str[-3:1:-1]

8. str[-4:-1:]

str[-4:-1:]

9. str[-4:2:]

str[-4:2:]

10. str[-2:-1:]

str[-2:-1:]

11. str[::-1]

str[::-1]

Originally published at https://github.com/vidyabhandary.

--

--