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

greeting=Good day!

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.

Step 1 of 10Called right: 0 of 6

Stuck? Write the position number under every character of greeting, starting at 0 and including the space and the !.

​

Computer Science · Index and length

One string, three views

  1. 1 Find
  2. 2 Explore
  3. 3 Spot the mismatch

Model. One short string drawn three ways: as code, as a table with a row for every character, and as a sentence. The table has a row for every character, spaces and punctuation included.

Step 1 · Find

In the index table, tap the cell that holds the index of the final character.

IndexCharacter
0H
1i
2
3
4S
5a
6m

Computer Science · Concatenation

What does + really do?

first = "Sam" last = "Lee" full = first + last and also: pin = "12" + "34"

Which idea is closest to what you think + does to strings?
How sure are you?

Computer Science · Building a string

Build a random password, line by line

Write a program that builds a password of 8 random characters, one character at a time. You supply the three lines that make it work.

  1. # Make an 8-character random passwordA comment to say what the program does.
  2. missing step
Which line is step 2?

Exam line: Build a string in a loop in three moves: import at the beginning, a blank string before the loop, and concatenate onto the end inside the loop.

Computer Science · Validation

Why was a right answer rejected?

Sam types TIGER, which is the right word typed in capitals. The program prints Wrong word. Click the line that causes it.

A student's validation program — which line goes wrong?

Exam line: Convert the input with lower() first, then compare the converted string. Comparing the original word loses the mark.

Computer Science · Exam question

Write it, then mark it

first = "Priya" surname = "PATEL"

A program makes a username. It takes the first three characters of first, joins them to surname converted to lower case, then outputs the username and its length. Write the program. [5 marks]

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

Exam line: Each technique is credited separately, so write the slice, the conversion, the join, the length and the output as separate steps.

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

1Index positions start at 0, and a space or a punctuation mark has a position like any letter.
2A slice stops before its end index, so word[0:3] has three characters.
3+ joins exactly. If you want a space, concatenate one.
4Blank string, loop, concatenate: that is the pattern for building a string, with import random at the top.

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?
Joining strings together to create a new string. The operator is the plus sign, +.
Does + put a space between two strings?
No. It joins them exactly as they are. Concatenate a space, " ", if you want one.
Which index is the first character of a string?
Index 0. Positions are counted from 0.
What does word[a:b] give?
The characters from index a up to, but not including, index b.
What does len() count?
Every character in the string, including spaces and punctuation.
What is the relationship between the last index and len()?
The last index is one less than the length.
What do upper() and lower() do?
upper() converts every letter to upper case. lower() converts every letter to lower case.
How do you build a new string inside a loop?
Before the loop, create a variable set to a blank string. Inside the loop, concatenate new data onto the end.
What does random.choice(string) do?
Picks one random character from the string, and the result can be different on each run.
Where does import random go?
At the beginning of the program, before random is used.
What is validation?
Checking that an input meets certain requirements. The contents of a string can be validated.
Why convert input with lower() before comparing it?
So a correct answer typed in capitals still matches the lower-case text you compare it with.
Which two techniques together make a random string, such as a password?
random.choice picks each character, and concatenation joins them on one at a time.

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

See the full Eduqas Computer Science curriculum →

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.