Skip to the page
Year 9 · Autumn 1 · Python II: lists and strings

Slicing and string methods

PracticalPresent ▶

What this lesson covers

Topic 14 Programming further

KS3 Computing · Real programs keep data in lists, take text apart as strings, split work into subroutines, check their input, save to files, and get tested on purpose.

ProgrammingCambridge Lower Secondary Computing 0860

You will learn to

  • Predict the result of a slice s[a:b], remembering that it stops before b.
  • Use s[::-1] to reverse a string.
  • Use upper(), find() and split(), and say what each one gives back.
  • Write a program that checks a postcode's shape, such as its length and which characters are digits.

Success criteria

  • I can predict s[0:3] and s[2:5] for a word before I run them.
  • I can say that find() gives -1 when the text is not there.
  • I can split a sentence into a list of words.
  • I can tell the user whether a postcode has the right shape.

Key words

Say each word, then read what it means. Every one is in the glossary too.

List
One name holding many values in order. Also called an array.
Index
An item's position in a list, counting from 0.
Subroutine
A named block of code you can call when needed.
Function
A subroutine that returns a value.
Parameter
A placeholder in a subroutine for a value passed in.
Validation
Checking input is reasonable before using it.
Boundary data
Test data at the very edge of what's allowed.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

Strings

A string is a list of characters, so it has indexes too. With word = "Computing":

CodeResult
len(word)9
word[0]"C"
word[0:3]"Com" (a slice: 0 up to, not including, 3)
word.upper()"COMPUTING"
"put" in wordTrue

Study it

Open the study page and go one step at a time.

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.