GCSE · Computer Science · Eduqas · Spec C500QS
String manipulation in code
Quick test: what does "Good day!" + "Sam" print? Python does exactly what the expression says, no more and no less, and that may not be what you expect.
Computer Science · Strings
Call it before it prints
Predict each marked line before it runs. Nothing is executed here: every result is written into the lesson, so you are checking your own thinking.
greeting = "Good day!"name = "Sam"print(greeting[0])print(greeting[6])print(greeting[5:8])print(greeting + name)print(greeting + " " + name)print(name.upper())print(greeting.lower())print(len(greeting))
Variables
Step 1. greeting is stored. Before you go on, write a position number under every character, starting at 0. The space and the ! get a number too.
Stuck? Write the position number under every character of greeting, starting at 0 and including the space and the !.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Python does exactly what the expression says to a string. Learn to say exactly what that is.
What you need to know
- A string is a row of characters, and every one counts: letters, spaces and punctuation.
- Each character has an index position, and the first one is at index 0, not 1.
Have a goMax counts the letters of "cat" like this: c is 1, a is 2, t is 3. He says index 1 is c. Is Max right?
No. Index 0 is c, so index 1 is a.
Max counts from 1, but index positions start at 0, so each index is one less than his count.
- word[i] picks out the single character at index i.
- A slice word[a:b] starts at index a and stops before index b, so the character at b is left out.
Have a goword = "planet". Work out what word[1:4] gives before you look.
lan
It takes indexes 1, 2 and 3 (l, a, n) and stops before index 4, so the e is not included.
- len(word) gives the number of characters in the string, spaces and punctuation included.
- Concatenation joins strings with +, exactly as they are, so no space appears unless you concatenate one.
Have a gofirst holds "Ann" and last holds "Lee". Write an expression that gives Ann Lee, with a space between.
first + " " + last
+ adds no space by itself, so a string containing a single space has to be concatenated between the two names.
- upper() converts every letter in a string to upper case, and lower() converts every letter to lower case.
- To build a string in a loop, make a blank string before the loop, then concatenate onto the end inside it.
- random.choice(string) picks a random character, and import random has to come first, at the start of the program.
- Validation checks that an input meets requirements, and the contents of a string can be validated.
The big picture
Strings are rows of characters. Python counts their positions from 0, a slice stops before its end index, + joins with no space added and len() counts everything. Put those together with a loop and the random module and you can build a whole new string.
Key points
Worked example
Problem
word = "keyboard". What does print(word[0:3].upper() + "-" + word[3:8]) output?
⚠ Watch out
Expecting + to put a space between words, or counting the first character as position 1. Python does neither: + joins exactly, and the first character is at index 0.
Memory hook
Start counting at 0, stop a slice one short, and glue with no gap: Python only ever does what the expression says.
Check yourself
Cover the page. word = "up, up!". Write the index under every character, spaces and punctuation too. Then say what word[0:2], word[4] and len(word) give.
Flashcards
(13)What is concatenation?
Does + put a space between two strings?
Which index is the first character of a string?
What does word[a:b] give?
What does len() count?
What is the relationship between the last index and len()?
What do upper() and lower() do?
How do you build a new string inside a loop?
What does random.choice(string) do?
Where does import random go?
What is validation?
Why convert input with lower() before comparing it?
Which two techniques together make a random string, such as a password?
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 Eduqas GCSE Computer Science topics
- Modular structuring with abstraction
- Presenting test outcomes with commentary
- Program a solution to requirements
- Refining code during development
- Selection in programs
- Sequence in programs
- Subroutines with well-defined interfaces
- Typical, extreme and erroneous test data
- Understanding revised requirements
- Using a variety of data types
- Using testing outcomes to inform development
- Validation and verification routines in code
How this lesson was checked. This Eduqas GCSE Computer Science (specification C500QS)lesson 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 16 November 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.