GCSE · Computer Science · OCR · Spec J277
Basic string manipulation (concatenation, slicing)
Ask Python to join cat and dog and you get catdog: no space, because nobody asked for one. Strings come out exactly as you write them.
Computer Science · Algorithms
Trace table — dry run the code
Step through the loop one line at a time. On each pass, predict what my_string will hold after the + runs, then press Next.
Ready when you are — step through one line at a time.
Case change
Commit to an answer first, then check it.
next_word holds "melon". What does print(next_word.upper()) display?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Join strings, slice them, change their case and pick from them at random. Python does exactly what you write.
What you need to know
- Concatenation means joining strings, or parts of strings, to make a new string. In Python the operator for it is +.
- It puts the strings straight together. Join cherry and drops and you get cherrydrops, with no space.
- Want a space? Concatenate a string containing a single space between them, like my_string = my_string + ' ' + item.
Have a goZara joins "ice" and "cream" with first_word + second_word and expects "ice cream". What does she actually get?
icecream, with no space.
The + adds no space of its own, so she has to concatenate a string containing a space in between.
- Build a string in a loop by starting with a blank string before the loop, then concatenating onto its end inside it.
- Slicing selects part of a string: word[start:end]. Counting starts at 0, so the first character is index 0.
- A slice stops before its end index. So word[0:3] takes indexes 0, 1 and 2, which is three characters.
Have a goword is "banana". Sam says word[1:4] takes four letters because the numbers go 1, 2, 3, 4. Which letters does it really take?
a, n, a (indexes 1, 2 and 3), so ana.
b is index 0, and the slice stops before the end index 4, so index 4 is not taken.
- Slices can be joined with +. With word_one = hello and word_two = world, word_two[2:4] + word_one[1:3] gives rlel.
- upper() and lower() convert all the letters to uppercase or lowercase. Write it as first_word.lower(): name, full stop, method, brackets.
- random.choice(string) picks a random character from a string. Write import random first, usually at the start of the program.
Have a goPriya makes random.choice("aeiou") the very first line of her program and Python complains. What should she write before it?
import random
The random module has to be imported before random.choice can be used, usually at the start of the program.
- Forget import random and Python stops with NameError: name 'random' is not defined.
The big picture
The + operator concatenates strings: it puts them straight together and adds no space of its own, so a space has to be concatenated in as a ' ' string. To build a string in a loop, start from a blank string before the loop and concatenate each new piece onto the end inside it. A slice word[start:end] counts from index 0 and stops before the end index, and slices can be joined with +. upper() and lower() convert all the letters to uppercase or lowercase, and random.choice(string), after import random, picks a random character.
Key points
Worked example
Problem
word = "sunflower". What does print(word[0:3] + ' ' + word[3:9]) display?
⚠ Watch out
Reading word[0:3] as four characters because the 3 is included, or as starting at the second letter because counting starts at 1. Python counts from 0 and a slice stops before its end index. And expecting + to add a space: it adds no space of its own.
Memory hook
Count from 0, stop before the end, and remember that + adds nothing of its own.
Check yourself
word = "sunflower". Without looking back, which characters does word[3:6] take and how many is that? Then say what print(word[0:3] + ' ' + word[3:6]) displays.
Flashcards
(12)What is concatenation, and which operator does Python use for it?
first_word is cherry and second_word is drops. What does first_word + second_word give?
How do you put a space between two concatenated strings?
Building a string with a loop: what comes before the loop, and what goes inside it?
In word[start:end], what index does the first character have?
Which indexes does word[0:3] take?
word is candy floss. What does word[8:11] select?
For hello and world, what does word_two[2:4] + word_one[1:3] give?
What do upper() and lower() do, and how is a call written?
How do you pick a random character from a string, and what else can the random module do?
Which error appears if you use random without importing it?
How does a program build a random string from several picks?
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 OCR GCSE Computer Science topics
How this lesson was checked. This OCR GCSE Computer Science (specification J277)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 14 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.