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.

1animals = ["cat", "dog", "mouse", "goat"]
2my_string = ""
3for item in animals:
4 my_string = my_string + item
5print(my_string)
item
my_string
Output
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Debug it

Where does the space go missing?

A student wants a space in front of every fruit name. Their program prints kiwilimeplum instead. Which line is at fault?

A student's program — which line goes wrong?

Slicing

What does word[0:3] pick out?

Here is word = "candy floss". It has eleven characters, counting the space. Someone runs print(word[0:3]).

Which idea is closest to what you think word[0:3] picks out?
How sure are you?

Slice and join

Two slices, one string

Let word_one be "hello" and word_two be "world". Work out what print(word_two[2:4] + word_one[1:3]) displays, one missing step at a time.

  1. word_one = "hello" word_two = "world"Number the characters from 0. hello: h 0, e 1, l 2, l 3, o 4. world: w 0, o 1, r 2, l 3, d 4.
  2. print(word_two[2:4] + word_one[1:3])Two slices joined by +. Work out each slice first, then join them.
  3. missing step
Which line is step 3?

Case change

Commit to an answer first, then check it.

next_word holds "melon". What does print(next_word.upper()) display?

Put it together

Build a random string

Write a short Python program that builds a three-character string: one random vowel, then one random consonant, then one random special character. Use any strings of vowels, consonants and special characters you like. [5 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1The + operator joins strings straight together and adds no space of its own. To get a space, concatenate a ' ' string in between.
2To build a string in a loop, set a blank string before the loop, then concatenate each new piece onto the end inside it.
3In word[start:end] the first character is index 0 and the slice stops before the end index.
4word[0:3] takes indexes 0, 1 and 2: three characters.
5Slices can be joined with +, in the order written.
6upper() and lower() convert all the letters. Write them as variable name, full stop, method name, brackets.
7random.choice(string) picks a random character, after import random.
8These are Python forms. If a question shows string handling in a different notation, use the notation it gives.

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?
Joining strings (and substrings) together to make a new string. In Python the operator is +.
first_word is cherry and second_word is drops. What does first_word + second_word give?
cherrydrops, with no space, because the strings are put straight together.
How do you put a space between two concatenated strings?
Concatenate a string containing a single space between them, for example my_string = my_string + ' ' + item.
Building a string with a loop: what comes before the loop, and what goes inside it?
Before: a variable set to a blank (empty) string. Inside: concatenate each new piece onto the end of it.
In word[start:end], what index does the first character have?
Index 0.
Which indexes does word[0:3] take?
0, 1 and 2. The slice stops before the end index, so that is three characters.
word is candy floss. What does word[8:11] select?
The last three letters, oss. The string has eleven characters, counting the space.
For hello and world, what does word_two[2:4] + word_one[1:3] give?
rlel. word_two[2:4] is rl, word_one[1:3] is el, and + joins them straight together.
What do upper() and lower() do, and how is a call written?
They convert all the letters to uppercase or lowercase. Variable name, full stop, method, brackets: print(first_word.lower()).
How do you pick a random character from a string, and what else can the random module do?
random.choice(string), after import random. The module can also generate random numbers and shuffle lists.
Which error appears if you use random without importing it?
NameError: name 'random' is not defined.
How does a program build a random string from several picks?
It concatenates each random.choice result onto a result string.

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 OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

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.