Strings – Slicing, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
Strings – Slicing in one minute
What students need to know
Omitting start defaults to 0. Slices from index 0 up to (not including) 3. Step -1 traverses the string backwards — a quick way to reverse.
Start at 0, stop before 3: P(0) y(1) t(2). [:2] starts from 0, stops before index 2: h(0) e(1).
Mistakes students make
- Use colon : for slicing, not comma.
- Step cannot be 0: causes ValueError.
- Use square brackets [] for slicing, not parentheses ().
- Trailing colon without step is fine but unnecessary: remove it for clarity.
PREVIOUS · MODULE 29Strings – Indexing: name[0]
YOU ARE HERE · MODULE 30Strings – Slicing
NEXT · MODULE 31String Methods (upper, lower, replace…)
What students will learn in this module
Every question in Module 30
Quick questions
- What does 'Python'[0:3] return?A'Pyth'B'Pyt'
- What does 'hello'[:2] return?A'he'B'hel'
- What does 'hello'[3:] return?A'llo'B'lo'
- Is the stop index included in a slice?ANo it stops before itBYes
- What does 'abcdef'[::2] return?A'ace'B'bdf'
Key terms
Students type the meaning of:
Slice[start:stop][:stop][start:]
Match the pairs
'hello'[1,4]Match it to what it does'hello'[10:20]Match it to what it does'hello'[:3]Match it to what it does'hello'[::-1]Match it to what it doesprint('hello'[1:4])Match it to what it doesprint('python'[2:])Match it to what it doesFill in the blanks
word = 'Python' print(word[_:3])
word = 'hello' print(word[:_])
word = 'hello' print(word[_:])
word = 'hello' print(word[::__])
text = 'abcdef' print(text[1:_])
Spot the error
Question 1
Aword = 'hello'Bpart = word[0,3]Cprint('Slice:', part)Dprint('Done')
Question 2
Aword = 'hello'Bpart = word[1:4:0]Cprint('Slice:', part)Dprint('Done')
Question 3
Aword = 'hello'Bpart = word(1:3)Cprint('Slice:', part)Dprint('Done')
Question 4
Aword = 'hello'Bpart = word[0:5:]Cprint('Slice:', part)D# trying to get all
Question 5
Apart = 'hello'[-1:-4]Bword = 'hello'Cprint('Slice:', part)Dprint('Done')
