Predict the Slice Output
Beginner · Predict the output · 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 lessonWhat does this code print? Think carefully about negative indices.
word = 'programming'
print(word[-4:])Write the code and run it to check your prediction.
# What does this print?
word = 'programming'
print(word[-4:])
More Slicing exercises
- Get the Middle ItemsBeginner
- Extract Even-Indexed ItemsIntermediate
- Reverse a WordBeginner
- Fix the Slicing BugIntermediate
- Palindrome CheckerAdvanced
All Python exercises · 50 practice problems · Data Structures quiz