KS3 · Computer Science

Working with strings in Python

To Python, a password like password1% is a row of characters. Learn to count it, search it, read any box, and meet the one thing you can't do to it.

Computing · Python strings

Read the boxes, then compare them

Two strings, six boxes each. Step through and watch what each line of Python produces.

Before each Next, say the value out loud. Then check.

1 language = "Python"
2 # P y t h o n
3 # 0 1 2 3 4 5
4 other = "Pascal"
5 first = language[0]
6 second = language[1]
7 same_start = language[0] == other[0]
8 differ = language[1] != other[1]
9 lengths_differ = len(language) != len(other)
10 no_z = "z" not in language
11 print(first, second)

Variables

language="Python"

Output

 

Step 1: The speech marks make this a string: six characters stored in order. The two # lines underneath are just labels for you: they number the boxes, starting from 0.

1 / 9

Use the numbered strip in the code (0 to 5) to find each index before you press Next.

Step 1 of 9: The speech marks make this a string: six characters stored in order. The two # lines underneath are just labels for you: they number the boxes, starting from 0..

Watch out: Index 1 is the SECOND character. The first character of a string lives at index 0.

Predict, then check

Commit to an answer before you look. Both lines hide a trap.

Python runs these two lines: print(len("Oak National Academy")) and then print("ty" in "Python"). What do they print?

What do you think?

What is inside the speech marks?

A program sets up two variables: password = "password1%" and code = "123".

Which is closest to what you think Python is storing in code?
How sure are you?

Find the bug

Which line breaks the program?

This program should turn "cat" into "bat" and print it. Instead, Python stops with an error. Which line causes it?

A program with one bug — which line goes wrong?

String or list?

StringvsList

They look alike, but one crucial rule is different. Start with the top row.

Focus

Can you change one part in place?

String

No. Strings are immutable: characters cannot be updated, replaced or added.

List

Yes. Lists are mutable: a value in the list can be updated without making a new list.

The insight

This is the difference that matters most. Square brackets let you READ a position in both, but only a list lets you WRITE into one.

What is it a sequence of?

String

Characters: letters, digits, spaces, punctuation and symbols.

List

Items: the values held in the list.

What does len count?

String

The number of characters. len("Python") is 6.

List

The number of items in the list.

Can you read one position by its index?

String

Yes, with square brackets. Counting starts at 0.

List

Yes, in the same way.

Computer Science · Algorithms

Trace table — dry run the code

Dry-run the loop one line at a time. Predict each row before you reveal it.

1word = "Hi!"
2index = 0
3while index < len(word):
4 print(word[index])
5 index = index + 1
index
word[index]
index < len(word)
Output
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

A string is a row of characters you can count, search and read, but never edit in place.

What you need to know

  • A string is a sequence of characters inside speech marks, and digits inside it are text, not numbers.
  • len counts a string's characters, and in checks whether one string sits inside another.
  • Each character has an index, starting at 0, and characters can be compared with ==, != and not in.
  • A while loop can check a string one character at a time.
  • Strings are immutable: to change one, you reassign the whole value.

The big picture

A Python string is a sequence of characters inside speech marks, and digits inside it are text. len counts its characters, spaces included; in checks for characters that sit side by side; and square brackets read one character by its index, starting at 0. Characters can be compared with ==, != and not in, and a while loop can check a string one character at a time. Strings are immutable, so to change one you reassign the whole value.

Key points

1A string is an ordered row of characters. Anything inside speech marks is text, digits included.
2len("Oak National Academy") is 20 because the two spaces are characters too.
3"on" in "Python" is True, but "ty" in "Python" is False: in needs the characters next to each other, in that order.
4Indexes start at 0, so for language = "Python", language[1] is "y".
5== means equal to, != means not equal to, and not in means the value is not held in the string.
6Assigning to one character, such as word[0] = "b", throws an error. Reassign the whole string instead: word = "bat".
7Lists are mutable and strings are immutable, but both are sequences, so len and indexing work on both.
8A while loop repeats until its condition stops it, so it can check a string one character at a time using an index that starts at 0.

Worked example

Problem

name1 = "Laura" and name2 = "Lauren". Work out the value of each expression: (a) name1[4] (b) name1[0] == name2[0] (c) len(name1) != len(name2) (d) "rn" in name2

⚠ Watch out

Counting from 1. In "Python", index 1 is y, not P. The first character is at index 0, so the last index is always one less than the length.

🧠

Memory hook

Boxes start at zero, and the boxes are glued shut. You can count them, search them and read them, but to change one you need a whole new string.

✓

Check yourself

greeting = "Hi there!". Predict len(greeting), greeting[3] and "he" in greeting. (Answers: 9, as the space and ! count; "t", as box 2 is the space; True.)

Flashcards

(14)
What is a string?
A data type that is a sequence of characters: letters, digits, spaces, punctuation and symbols such as $ or @.
How do you show Python that something is a string?
Put speech marks around the characters, e.g. name = "Laura".
In code = "123", are the digits numbers?
No. Digits inside a string are text characters. "123" is three characters.
What does len do to a string?
It returns the number of characters. Spaces count, so len("Oak National Academy") is 20.
What does len count for a list?
The number of items in the list.
What does the in operator give you?
True or False: whether one string is contained in another.
Why is "ty" in "Python" False?
There is no t followed directly by y. in needs the characters next to each other, in that order.
Which index is the first character of a string?
Index 0. Python indexing is zero-based.
language = "Python". What is language[1]?
"y", the second character.
What does immutable mean?
Not able to be changed. A string's characters cannot be changed, added or removed.
What happens if you assign a new value to one character of a string?
Python throws an error, because strings are immutable.
How do you change a string, then?
Rewrite it: reassign the whole value to the variable, which overwrites the old one.
Mutable or immutable: list? string?
A list is mutable (values can be updated in place). A string is immutable.
What do ==, != and not in mean?
== equal to; != not equal to; not in means the value is not held in the string.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More KS3 Computer Science topics

See the full KS3 Computer Science curriculum →

How this lesson was checked. This KS3 Computer Sciencelesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 2 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.