KS3 · Computer Science

Working with strings in Python

To you, password1% is a password. To Python, it's ten characters in numbered boxes, and there's one thing Python won't let you do to them.

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: Speech marks around the characters make a string. Inside them: six characters, P-y-t-h-o-n. The two # lines are comments that 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: Speech marks around the characters make a string. Inside them: six characters, P-y-t-h-o-n. The two # lines are comments that number the boxes, starting from 0..

Watch out: Index 1 is the second character, not the first. Python has six characters, so its boxes are numbered 0 to 5.

Predict, then check

Commit before you look. Both lines have a trap in them.

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

What do you think?

What's 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 rule is completely different. Start with the top row.

Focus

Can you change one part of it?

String

No. Strings are immutable: a character can't be updated, replaced or added.

List

Yes. Lists are mutable: a value can be updated without creating a new list.

The insight

This is the one people mix up. 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.

List

Items: the values held in the list.

What does len count?

String

The characters, spaces included.

List

The 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 not change one box at a time.

What you need to know

  • A string is a data type: a sequence of characters, like letters, digits, spaces, punctuation and symbols such as $ or @.
  • In Python, speech marks around the characters make a string: name = "Laura".
  • Digits inside a string are text characters, not a number, so "123" is treated as text.
  • len gives the number of characters, and spaces count: len("Python") is 6.
  • Have a goCount before you peek: what is len("hi there")?

    8

    h and i, one space, then t-h-e-r-e: 2 + 1 + 5 = 8. If you got 7, you skipped the space, and spaces are characters too.

  • The in operator gives True or False, and the characters you search for must sit next to each other.
  • Have a goYour friend bets you a biscuit that "cat" in "the cart" is True, because c, a and t are all in there. Do you win the biscuit?

    Yes. It's False.

    In "the cart" an r sits between the a and the t, so c, a, t never appear side by side. Having all the letters somewhere isn't enough.

  • Square brackets read one character by its index, and counting starts at 0: language[0] is the first character.
  • Have a gopet = "rabbit". Which index number reads the first b?

    2

    r is index 0, a is 1, b is 2. Saying 3 means you counted from 1.

  • Compare characters with == (equal), != (not equal) and not in (not held in the string).
  • Strings are immutable: assigning a new value to one character, like word[0] = "b", throws an error.
  • Need a change? Rewrite the whole string and reassign it to the variable. Lists are different: they're mutable.
  • A while loop repeats while its condition holds, so it can step through a string one index at a time.

The big picture

A string is a data type: a sequence of characters, made in Python by putting speech marks around them. Digits inside a string are text, not a number. len counts the characters (spaces too), in checks whether one string sits inside another with its characters next to each other, and square brackets read one character by its index, starting from 0. ==, != and not in compare characters and lengths. Strings are immutable, so you can't assign to one character; you rewrite the whole string and reassign the variable. Lists, by contrast, are mutable. A while loop with an index can check a string character by character.

Key points

1A string is a sequence of characters of any kind; speech marks around the characters make it a string in Python.
2Digits in a string are text: "123" is three characters, not a number.
3len counts characters, spaces included: len("Oak National Academy") is 20.
4"on" in "Python" is True; "ty" in "Python" is False because t and y aren't next to each other.
5Indexing is zero-based: language[0] is the first character and language[1] the second.
6==, != and not in compare characters, and len(a) != len(b) compares lengths.
7Strings are immutable: assigning to one character throws an error, so you rewrite the whole string and reassign the variable.
8Lists are mutable and strings aren't, but both are sequences, so len and indexing work on each.
9A while loop with index < len(word) can check a string one character at a time.

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 0 is P and index 1 is y, so if you said language[1] is P, you started at 1. Six characters means the boxes are numbered 0 to 5.

🧠

Memory hook

Count from zero, and the string is sealed. You can read any box, but to change the words, write the whole string out again.

✓

Check yourself

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

Flashcards

(14)
What is a string?
A data type: a sequence of characters, such as letters, digits, spaces, punctuation and symbols like $ or @.
How do you make a string in Python?
Put speech marks around the characters, e.g. name = "Laura".
code = "123". Are those digits a number?
No. They're text characters. A string can hold numerical values, but they're treated as text.
What does len give you for a string?
The number of characters, spaces included. (For a list, len gives the number of items.)
What does the in operator give you?
True or False: whether one string is contained within another.
Why is "ty" in "Python" False?
Because t and y are not next to each other in "Python". in needs the characters side by side.
What index does the first character of a string have?
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 can't be changed, added or removed.
What happens if you assign a new value to one character of a string?
Python throws an error.
If a string needs to change, what do you do?
Rewrite the whole string and reassign it to the variable. The new value overwrites the old one.
Mutable or immutable: a list? a string?
List: mutable (values can be updated in place). String: immutable.
What do ==, != and not in mean?
== equal to; != not equal to; not in means the value is not held in the string.
What does the condition index < len(word) do in a while loop?
It keeps the loop running only while index is less than the length, so index takes the values 0 up to one less than the length.

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

Helpful guides for parents and students