🛰️ Python, Grade 8 · session 5 of 16 · 60 minutes · 🎁 free with an account

Strings: slicing and methods

Every message NOVA sends is a string: a row of characters, each with a number. Today you cut strings into pieces with slices, flip them backwards, and use string methods to clean up messy crew logs and decode secret signals.

Illustration of a cute pastel robot dancing
Illustration: andremsdesign / Pixabay

A complete lesson · 60 minutes

Teach it, see it, practise it, all on this page.

Open this page with your learner and work down it, or press Teach this lesson to go through it one step at a time on a big screen. Every section has something to do.

  1. Warm-up3 min
  2. Warm-up games5 min
  3. Strings are rows of characters9 min
  4. Slicing: s[start:stop:step]9 min
  5. String methods: clean, search, split and join9 min
  6. Username maker7 min
  7. Secret signal decoder7 min
  8. Word statistics5 min
  9. Quiz5 min
  10. Set the after-class project1 min

By the end you can

  • Find a character by its index, counting from 0 or from -1 at the end
  • Slice with s[start:stop:step], and reverse with s[::-1]
  • Clean and search text with upper, lower, strip, replace, count and find
  • Split a sentence into words and join words back into a sentence

Before you start

  • for loops (lesson 3)
  • if and in (lesson 2)

Have ready

  • A computer or tablet with this page open
  • Squared paper to draw index boxes

Grade 8 · ages 13 and 14 · no experience needed

Warm-up · think first

NOVA's signal reads AVONREPUS. Read it backwards. What is it?

Show the answer

SUPERNOVA. Today you will make Python flip it with one slice: signal[::-1].

Teacher tip: Ask how they read it backwards: one letter at a time from the end. That's exactly what step -1 does.

More warm-ups for every class →

🎮 Warm-up games · 5 quick games

Play with Bolt the robot

Five minutes with NOVA, the station computer. Vote, say why, then show the answer.

  1. Which one is NOT the first letter of its word: 'NOVA'[0], 'MOON'[0], 'SPACE'[1], 'ORBIT'[0]? ('SPACE'[1] · 'NOVA'[0] · 'MOON'[0] · 'ORBIT'[0])
    Answer: 'SPACE'[1]. It is P, the second letter; the other three use index 0, the first letter.
  2. len('star') is 4. (Always · Sometimes · Never)
    Answer: Always — Always: s, t, a, r is four characters. The last one is at index 3, because counting starts at 0.
  3. Would you rather decode RETUPMOC or 3ARC4? (RETUPMOC · 3ARC4)
    Answer: RETUPMOC is COMPUTER backwards: one slice, [::-1], fixes it. Numbers mixed in need replace.
  4. How many letters are in the word 'mississippi', and how many of them are s?
    Answer: 11 letters, and 4 of them are s. Python: len('mississippi') and 'mississippi'.count('s').
  5. Where does your phone cut, clean or search text for you?
    Answer: Autocorrect, search bars, hashtags, usernames that ignore capitals, trimming spaces from passwords… all string methods.

Three levels, like a real engineer

Use it. Tweak it. Build it.

First use what the lesson shows exactly as it is, then change one key thing and see what happens, then make your own.

  1. 🔒

    Level 1 · Use it

    Run it as it is

    Run the lesson's example program exactly as written and watch what it prints or draws.

  2. 🔧

    Level 2 · Tweak it

    Change one value

    Change one number or word in that program. Say what will happen first, then run it and check.

  3. 🛠️

    Level 3 · Build it

    Write your own

    Write a short program of your own that uses today's idea, and test it until it works.

Part 1 of 3 · learn, see, practise

Strings are rows of characters

A string is a row of boxes, one character in each. I number them from 0 on the left. I can also count from the end: -1 is the last box.

A string is a sequence: every character has a position called its index. The first character is at index 0, the second at 1, and so on. s[0] is the first character of s.

Negative indexes count from the end: s[-1] is the last character, s[-2] the one before it. They are handy when you don't know how long the string is.

len(s) is the number of characters, spaces included. The last index is always len(s) - 1, so s[len(s)] is an IndexError: there is no box there.

Strings can't be changed in place: s[0] = 'X' is an error. To change a string you build a new one, for example with slicing or methods, and store it again.

Remember!
  • The first index is 0; the last is len(s) - 1.
  • s[-1] is the last character.
  • Strings can't be changed in place: make a new one.
📝 Notes

for ch in s: goes through the characters one by one.

ch in s checks whether ch appears anywhere in s.

Spaces and punctuation are characters too: len('hi there') is 8.

🧑‍🏫 Teacher: type this live with the class

NOVA's name tag

  1. name = "SUPERNOVA"

    A string of 9 characters.

  2. print(name[0])

    Index 0: the first letter, S.

  3. print(name[-1])

    Index -1: the last letter, A.

  4. print(len(name))

    9 characters.

  5. print(name[4])

    Ask: which letter is at index 4? Count from 0: R.

Type here

▶ Output

(nothing is printed)

📦 Memory boxes

No variables yet
Example: run it, then change it

▶ Output

0 S
1 T
2 A
3 T
4 I
5 O
6 N
Last: N | Length: 7

📦 Memory boxes

word'STATION'stri6int

Press 🔍 Dry run to watch i and word[i] change together. Then try a word of your own.

Practise it

Start with the warm-up and go as far as you can. The stretch questions are meant to be hard.

Warm-up

1.1 Print the first and the last character of code, on separate lines.

Task 1.1

▶ Output

(nothing is printed)

📦 Memory boxes

code'ORBIT'str
🧑‍🏫 Model answer (for the teacher)

Teacher eyes only: hide the answers before you share your screen.

The answer green = the 2 lines the student writes

code = "ORBIT"print(code[0])print(code[-1])

What it prints

O
T

What ✓ Check looks for

  • it prints exactly:
    O
    T
  • With code = "MOON", it prints exactly:
    M
    N
Warm-up

1.2 Print how many characters are in msg, including the spaces.

Task 1.2

▶ Output

(nothing is printed)

📦 Memory boxes

msg'hello crew'str
🧑‍🏫 Model answer (for the teacher)

Teacher eyes only: hide the answers before you share your screen.

The answer green = the 1 line the student writes

msg = "hello crew"print(len(msg))

What it prints

10

What ✓ Check looks for

  • it prints exactly:
    10
  • In the code: Use len().
Core

1.3 Print the middle character of word (its length is odd).

Task 1.3

▶ Output

(nothing is printed)

📦 Memory boxes

word'PLANETS'str
🧑‍🏫 Model answer (for the teacher)

Teacher eyes only: hide the answers before you share your screen.

The answer green = the 1 line the student writes

word = "PLANETS"print(word[len(word) // 2])

What it prints

N

What ✓ Check looks for

  • it prints exactly:
    N
  • With word = "ABC", it prints exactly:
    B
Core

1.4 Count the letter a in log with a loop (no count method) and print the count.

Task 1.4

▶ Output

(nothing is printed)

📦 Memory boxes

log'data and banana'str
🧑‍🏫 Model answer (for the teacher)

Teacher eyes only: hide the answers before you share your screen.

The answer green = the 5 lines the student writes

log = "data and banana"count = 0for ch in log:    if ch == "a":        count += 1print(count)

What it prints

6

What ✓ Check looks for

  • it prints exactly:
    6
  • In the code: Use a for loop over the characters.
Stretch 🔥

1.5 Print every character of word with its index, as 0:S 1:K 2:Y on one line.

Task 1.5

▶ Output

(nothing is printed)

📦 Memory boxes

word'SKY'str
🧑‍🏫 Model answer (for the teacher)

Teacher eyes only: hide the answers before you share your screen.

The answer green = the 4 lines the student writes

word = "SKY"parts = []for i in range(len(word)):    parts.append(f"{i}:{word[i]}")print(" ".join(parts))

What it prints

0:S 1:K 2:Y

What ✓ Check looks for

  • it prints exactly:
    0:S 1:K 2:Y
  • With word = "GO", it prints exactly:
    0:G 1:O
Stretch 🔥

1.6 Mix with lesson 2: print Vowel if the first letter of name is a vowel, otherwise Consonant.

Task 1.6

▶ Output

(nothing is printed)

📦 Memory boxes

name'Orion'str
🧑‍🏫 Model answer (for the teacher)

Teacher eyes only: hide the answers before you share your screen.

The answer green = the 4 lines the student writes

name = "Orion"if name[0].lower() in "aeiou":    print("Vowel")else:    print("Consonant")

What it prints

Vowel

What ✓ Check looks for

  • it prints exactly:
    Vowel
  • With name = "Mars", it prints exactly:
    Consonant
  • With name = "Europa", it prints exactly:
    Vowel

🎁 Free for a year

Make a free account to open the rest of this lesson

Every new account opens every lesson of every course free for a whole year: no payment, no card, nothing to cancel.

Unlocking the rest of this lesson gives you:

  • Part 2: Slicing: s[start:stop:step], with practice at three levels
  • Part 3: String methods: clean, search, split and join, with practice at three levels
  • Class activity 1: Username maker
  • Class activity 2: Secret signal decoder
  • Class activity 3: Word statistics
  • A 8-question quiz
  • After-class project: Crew name badge generator
  • Homework and a printable practice sheet with answers
  • Teach mode and the minute-by-minute teacher's plan

It takes a minute. Anyone under 13 signs up with a parent. The first 2 lessons of every course, every game and all maths practice stay free.

FAQ

Strings: slicing and methods: questions

How does string slicing work in Python?

s[start:stop:step] takes characters from index start up to, but not including, stop, jumping by step. Leave start or stop out to mean the beginning or the end, and use step -1 to go backwards.

How do I reverse a string in Python?

Use a slice with a step of -1: s[::-1]. It walks the string from the end to the start.

Start a conversation

Want a teacher to take this class?

Tell us the learner's age and level. A teacher from our network teaches with these lessons; we confirm timing and price first.

  • Tell us the learner’s age or grade and goals
  • We’ll suggest a suitable class or learning path
  • We’ll confirm format, timing and fees with a parent or guardian

For a child’s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

We’ll use your details only to respond to this request.

Chat on WhatsApp
Esc

Try