Strings – Slicing, online

Six activities your students do on their own devices. See who's finished, and their scores.

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.

Discover other journeys with this module

What students will learn in this module

Every question in Module 30

Quick questions

  1. What does 'Python'[0:3] return?A'Pyth'B'Pyt'
  2. What does 'hello'[:2] return?A'he'B'hel'
  3. What does 'hello'[3:] return?A'llo'B'lo'
  4. Is the stop index included in a slice?ANo it stops before itBYes
  5. 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 does
print('hello'[1:4])Match it to what it does
print('python'[2:])Match it to what it does

Fill in the blanks

  1. word = 'Python'
    print(word[_:3])
  2. word = 'hello'
    print(word[:_])
  3. word = 'hello'
    print(word[_:])
  4. word = 'hello'
    print(word[::__])
  5. 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')