Skip to content
dreamcode
dreamcode
Map
Slicing
Lesson 33 of 77
+15 XP on finish
PYTHON INTERMEDIATEChapter 6 · Comprehensions and data tools

Slicing sequences

Slicing extracts a portion of a list or string using [start:stop:step]. Omitted values default to the beginning, end, or a step of 1.

Worked example

How it reads

  • nums[1:4] gets index 1 up to (but not including) index 4
  • nums[::-1] reverses the sequence
Cloud tip: Negative indices count from the end of the list, e.g., nums[-1] is the last item.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Print the word reversed using a slice with a negative step: edocmaerd.

Press Run to check your work.