Beginner·5 min·basics · slicing
Slicing
Slicing
seq[start:stop:step] — any of the three can be omitted.
Rules
startdefaults to0,stoptolen(seq),stepto1- Negative indices count from the end (
-1is the last) - The slice never raises on out-of-range — it just clamps
Slicing returns a copy, not a view
b = a[:] is the idiomatic shallow copy of a list.
Try it
- Take every third character of
"abcdefghij". - Get the middle third of a 30-element list.