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 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: 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.
Use the numbered strip in the code (0 to 5) to find each index before you press Next.
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?
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 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
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?
How do you make a string in Python?
code = "123". Are those digits a number?
What does len give you for a string?
What does the in operator give you?
Why is "ty" in "Python" False?
What index does the first character of a string have?
language = "Python". What is language[1]?
What does immutable mean?
What happens if you assign a new value to one character of a string?
If a string needs to change, what do you do?
Mutable or immutable: a list? a string?
What do ==, != and not in mean?
What does the condition index < len(word) do in a while loop?
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
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.