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 n3 # 0 1 2 3 4 54 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 language11 print(first, second)
Variables
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.
Use the numbered strip in the code (0 to 5) to find each index before you press Next.
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?
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.
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
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?
How do you show Python that something is a string?
In code = "123", are the digits numbers?
What does len do to a string?
What does len count for a list?
What does the in operator give you?
Why is "ty" in "Python" False?
Which index is the first character of a string?
language = "Python". What is language[1]?
What does immutable mean?
What happens if you assign a new value to one character of a string?
How do you change a string, then?
Mutable or immutable: list? string?
What do ==, != and not in mean?
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 postedMore KS3 Computer Science topics
- Abstraction in computational thinking
- Adding binary numbers
- Binary to denary conversion
- Boolean logic: AND, OR, NOT
- Bubble sort
- Building truth tables
- Client-server vs peer-to-peer
- Collecting and recording data
- Comparing sorting algorithms
- Compressing data
- Creating a 3D animation
- Decomposition: splitting problems up
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.