Get the Middle Items
Beginner · Write the code · from Python Slicing: Master Indexing for Strings, Lists, and Tuples
From the lesson: Python Slicing: Master Indexing for Strings, Lists, and Tuples
Imagine a pizza cut into 8 slices, numbered 0 through 7. You don't always want the whole pizza. Sometimes you want slices 2 through 5. Sometimes you want every other slice. Sometimes you want the last three slices.
Read the lessonGiven the list animals, use slicing to print only the middle three items: ['cat', 'dog', 'elephant'].
animals = ['ant', 'bear', 'cat', 'dog', 'elephant', 'fox', 'giraffe']
# Print the middle three items using slicing
More Slicing exercises
- Predict the Slice OutputBeginner
- Extract Even-Indexed ItemsIntermediate
- Reverse a WordBeginner
- Fix the Slicing BugIntermediate
- Palindrome CheckerAdvanced
All Python exercises · 50 practice problems · Data Structures quiz