Year 9 · Autumn 1

Python II: lists and strings · Lesson 4 of 5

Slicing and string methods

PracticalTopic 14 Programming further

Year 9 · Python II: lists and strings · Lesson 4 of 5

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.

Year 9 · Python II: lists and strings · Lesson 4 of 5

Success criteria

You have got it when you can say:

  • 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.

Year 9 · Python II: lists and strings · Lesson 4 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last lesson

    Typing your new password twice so they can be compared is…

    A ValidationB AuthenticationC Verification

    C. Verification

  2. Earlier this unit

    In def area(w, h): … and a = area(4, 3), which are the arguments?

    A 4 and 3B area and aC w and h

    A. 4 and 3

  3. Last year

    Testing a 0–50 range with 50 is…

    A Erroneous dataB Boundary dataC Normal data

    B. Boundary data

Year 9 · Python II: lists and strings · Lesson 4 of 5

Key words (1 of 2)

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.

Year 9 · Python II: lists and strings · Lesson 4 of 5

Key words (2 of 2)

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 · step 1 of 1

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

Year 9 · Python II: lists and strings · Lesson 4 of 5

Quick quiz · 1 of 3

open("log.txt", "w") on a file that already holds 100 lines…

  1. A Wipes it first
  2. B Gives an error
  3. C Adds to the end

A. Wipes it first

"w" empties the file before writing. Use "a" to append.

Year 9 · Python II: lists and strings · Lesson 4 of 5

Quick quiz · 2 of 3

The program runs but gives the wrong answer. That's a…

  1. A Syntax error
  2. B Runtime error
  3. C Logic error

C. Logic error

The code is valid and doesn't crash; the thinking is wrong. Only testing finds it.

Year 9 · Python II: lists and strings · Lesson 4 of 5

Quick quiz · 3 of 3

Program runs but gives the wrong answer. That's a…

  1. A Syntax error
  2. B Logic error

B. Logic error

The code is valid; the thinking is wrong.

Year 9 · Python II: lists and strings · Lesson 4 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. In ["a", "b", "c"], the index of "c" is…

    A 2B cC 3

    A. 2 Counting starts at 0: a = 0, b = 1, c = 2.

  2. word = "Python". What is word[1:4]?

    A "ytho"B "yth"C "Pyth"

    B. "yth" Start at index 1 (y), stop before index 4 (o): y, t, h. That's 4 − 1 = 3 characters.