SLIDE 1
CSZone.co.uk
Click to reveal · Arrow keys also work
OCR J277 · Component 2 · Topic 2.2.1e

Programming Techniques
String Manipulation

Length, indexing, substring, case conversion, ASCII — in OCR ERL and Python

CSZone OCR GCSE Computer Science J277
Learning Objectives

By the end of this video you will be able to...

Use concatenation to join strings together and explain how strings are stored as a sequence of characters
Find the length of a string using length / len(), and access individual characters using 0-based indexing
Extract part of a string using substring() in OCR ERL and slicing in Python
Convert string case using upper() and lower(), and convert between strings and numbers using str(), int(), float()
Use ASC() and CHR() in OCR ERL (and ord() / chr() in Python) to work with ASCII character codes
⚡ String manipulation lets programs process text — usernames, passwords, search terms, output formatting — all rely on these operations.
Strings

What is a string — and concatenation

WHAT IS A STRING?
A string is a sequence of characters enclosed in speech marks. Characters include letters, digits, spaces, and symbols. Each character occupies one position in the sequence, numbered from index 0.
EXAMPLE — "Hello"
H
e
l
l
o
0
1
2
3
4
5 characters — indices 0 to 4
OCR ERL — STRING DECLARATION
greeting"Hello" name"Alice" digit"5" ← this is a string, not an int!
CONCATENATION
Concatenation joins two or more strings together using the + operator. The strings are joined end-to-end in the order given. You can concatenate string variables, string literals, and the results of string functions.
OCR ERL — CONCATENATION
first"Hello" second" World" resultfirst + second print(result) ← outputs: Hello World // Concatenate with a literal: name"Alice" print("Hello, " + name + "!") ← outputs: Hello, Alice!
CANNOT MIX TYPES WITH +
"Age: " + 16 causes an error — you cannot use + to join a string and an integer. Cast the integer first: "Age: " + str(16)
String Operations

String length and character indexing

LENGTH
myString.length (OCR ERL) or len(myString) returns the total number of characters in the string — including spaces and symbols.
OCR ERL — LENGTH
word"Computer" print(word.length) ← outputs 8 print(len(word)) ← also outputs 8 sentence"Hi there" print(sentence.length) ← outputs 8 (space counts!)
PYTHON — LENGTH
word = "Computer" print(len(word)) # outputs 8
CHARACTER INDEXING — 0-BASED
Access a single character using its index in square brackets. The first character is always at index 0. The last character is at index length − 1.
"Computer" — index diagram
C
o
m
p
u
t
e
r
0
1
2
3
4
5
6
7
word"Computer" print(word[0]) ← "C" print(word[3]) ← "p" print(word[7]) ← "r" (last char)
⚡ The last character index is always length − 1, not length. For "Computer" (length 8), the last character "r" is at index 7. This is a classic off-by-one error in the exam.
String Operations

Extracting a substring

substring() — OCR ERL
myString.substring(start, length) returns a portion of a string. start is the 0-based index of the first character to extract. length is how many characters to return.
OCR ERL — SUBSTRING
word"Computer" // First 4 characters: print(word.substring(0, 4)) ← "Comp" // 3 characters from index 3: print(word.substring(3, 3)) ← "put" // Last character only: print(word.substring(7, 1)) ← "r"
"Computer" — substring visualised
C
o
m
p
u
t
e
r
0
1
2
3
4
5
6
7
substring(0, 4) → "Comp"
PYTHON — SLICING
word = "Computer" # Slicing syntax: string[start:end] # 'end' is EXCLUSIVE — not included print(word[0:4]) # "Comp" (0,1,2,3) print(word[3:6]) # "put" (3,4,5) print(word[:4]) # "Comp" (from start) print(word[4:]) # "uter" (to end)
OCR ERL vs PYTHON — KEY DIFFERENCE
OCR ERL substring(start, length) — second argument is how many characters.
Python [start:end] — second number is the stop index (exclusive), not the length.
USING substring() WITH length
word"Computer" // Get the last 3 characters: last3word.substring(word.length - 3, 3) print(last3) ← "ter" // Get first character: initialword.substring(0, 1) print(initial) ← "C"
String Operations

Case conversion — upper() and lower()

upper() AND lower()
myString.upper() returns a new string with all letters converted to UPPERCASE. myString.lower() returns a new string with all letters converted to lowercase. The original string is not changed — a new string is returned.
OCR ERL — CASE CONVERSION
word"Hello World" print(word.upper()) ← "HELLO WORLD" print(word.lower()) ← "hello world" print(word) ← "Hello World" (unchanged!)
PYTHON EQUIVALENT
word = "Hello World" print(word.upper()) # "HELLO WORLD" print(word.lower()) # "hello world"
REAL-WORLD USE — CASE-INSENSITIVE COMPARISON
// Without case conversion — fragile: answerinput("Continue? (yes/no): ") IF answer == "yes" THEN ← "Yes" fails! print("Continuing...") ENDIF // With lower() — robust: answerinput("Continue? (yes/no): ") IF answer.lower() == "yes" THEN print("Continuing...") ENDIF ← "Yes", "YES", "yEs" all work
WHY THIS MATTERS IN THE EXAM
Converting user input to lower() or upper() before comparing is a common exam technique tied to defensive design (Topic 2.3.1). It handles unexpected capitalisation from the user — a mark-winning detail in "write a program" questions.
String Operations

Type conversion — str(), int(), float()

WHY TYPE CONVERSION IS NEEDED
User input always arrives as a string — even if the user types a number. To do arithmetic, you must cast it to an integer or float. To output numbers in a sentence, you must cast them back to a string.
OCR ERL — TYPE CONVERSION
// String → integer: ageStrinput("Enter age: ") ageint(ageStr) print(age + 1) ← numeric addition // String → float: pricefloat(input("Price: ")) // Integer → string: score95 print("Score: " + str(score))
PYTHON EQUIVALENT
age = int(input("Enter age: ")) price = float(input("Price: ")) score = 95 print("Score: " + str(score))
CONVERSION FUNCTIONS — QUICK REFERENCE
FunctionConvertsExample
int()str/float → integerint("5") → 5
float()str/int → floatfloat("3.14") → 3.14
str()int/float → stringstr(42) → "42"
NOTE
int("3.7") raises an error — you cannot convert a decimal string directly to int. Cast to float first: int(float("3.7")) → 3
⚡ A very common pattern in exam programs: age ← int(input("Age: ")) — combines input and conversion in one line. Both OCR ERL and Python accept this style.
String Operations

ASCII codes — ASC() and CHR()

WHAT IS ASCII?
ASCII (American Standard Code for Information Interchange) assigns a unique number to each character. Computers store characters as numbers. Every letter, digit, and symbol has an ASCII code — 'A' = 65, 'a' = 97, '0' = 48.
OCR ERL — ASC() AND CHR()
// ASC() → returns ASCII code of a char: print(ASC("A")) ← 65 print(ASC("a")) ← 97 print(ASC("0")) ← 48 // CHR() → returns char from ASCII code: print(CHR(65)) ← "A" print(CHR(98)) ← "b"
PYTHON — ord() AND chr()
print(ord("A")) # 65 print(chr(65)) # "A" print(ord("a")) # 97
KEY ASCII VALUES TO KNOW
CharCodeNotes
'0' – '9'48 – 57Digit chars
'A' – 'Z'65 – 90Uppercase
'a' – 'z'97 – 122Lowercase
USING ASC() FOR CIPHER PROBLEMS
// Shift letter by 1 (Caesar cipher style): ch"A" shiftedCHR(ASC(ch) + 1) print(shifted) ← "B"
⚡ The difference between 'A' (65) and 'a' (97) is exactly 32. Lowercase letters always have higher ASCII codes than their uppercase equivalents. In OCR ERL, ASC() takes a single character — not a full string.
String Operations

Useful string operations — comparison and search

STRING COMPARISON
Strings can be compared using ==, !=, and even < / > (alphabetical order based on ASCII values). Comparison is case-sensitive — "Hello" ≠ "hello".
word1"apple" word2"Apple" IF word1 == word2 THEN print("Same") ELSE print("Different") ← this runs ENDIF // Case-insensitive compare: IF word1.lower() == word2.lower() THEN print("Same") ← this runs ENDIF
CHECKING FIRST CHARACTER
// Check if string starts with "A": name"Alice" IF name[0] == "A" THEN print("Starts with A") ENDIF
PYTHON — ADDITIONAL STRING METHODS
text = "Hello World" # Check if substring present: print("World" in text) # True print("world" in text) # False (case-sensitive) # Find position of substring: print(text.find("World")) # 6 # Replace a substring: print(text.replace("World", "Alice")) # "Hello Alice" # Strip whitespace: print(" hi ".strip()) # "hi"
IN THE EXAM
For OCR ERL pseudocode, the examiners expect: length, substring(), upper(), lower(), ASC(), CHR(), and indexing with [i]. Python methods like find() and replace() only appear in Python-specific questions.
Worked Example

String manipulation — exam-style problem

PROBLEM
Write OCR ERL pseudocode that inputs a user's first name and surname separately, then outputs their full name (first + space + surname), the total number of characters (including the space), and their initials (first character of each name).
OCR ERL SOLUTION
firstinput("First name: ") surnameinput("Surname: ") // Build full name: fullNamefirst + " " + surname print("Full name: " + fullName) // Total character count: totalfullName.length print("Characters: " + str(total)) // Initials: initialsfirst[0] + "." + surname[0] + "." print("Initials: " + initials)
PYTHON SOLUTION
first = input("First name: ") surname = input("Surname: ") # Full name: fullName = first + " " + surname print("Full name: " + fullName) # Character count: print("Characters: " + str(len(fullName))) # Initials: initials = first[0] + "." + surname[0] + "." print("Initials: " + initials)
OPERATIONS USED IN THIS EXAMPLE
Concatenation — joining first, " ", and surname with +
length — total characters in the full name including the space
Indexing [0] — extract the first character of each name
str() — cast the integer total to string for concatenation
Exam Practice

String manipulation — exam questions

Question 1 — 1 mark
A variable is declared as: word ← "Science"
What is the value of word[2]?
Answer — Q1
S
c
i
e
n
c
e
0
1
2
3
4
5
6
word[2] = "i" — strings are 0-indexed, so index 2 is the third character. (1 mark)
Question 2 — 2 marks
Write OCR ERL to output the first 3 characters of a string stored in a variable called word, then output its full length.
Answer — Q2
print(word.substring(0, 3)) ← [1] print(str(word.length)) ← [1]
Mark 1: correct substring(0, 3). Mark 2: word.length or len(word) output.
Question 3 — 4 marks
Write OCR ERL pseudocode for a program that:
• inputs a word from the user
• outputs the word in uppercase
• outputs the first character of the word
• outputs the length of the word
Exam Answers

Question 3 — answer and mark scheme

Q3 MARK SCHEME
wordinput("Enter a word: ") ← [1] print(word.upper()) ← [1] print(word[0]) ← [1] print(str(word.length)) ← [1]
Mark 1: correct input into a variable.
Mark 2: word.upper() used in print.
Mark 3: word[0] to get first character.
Mark 4: word.length or len(word) correctly output.
COMMON MARK LOSSES ON THIS Q
• Using word[1] instead of word[0] — classic off-by-one
• Forgetting str() when concatenating length with a string
• Writing word.upper without the parentheses — in OCR ERL, functions need ()
PYTHON EQUIVALENT
word = input("Enter a word: ") print(word.upper()) print(word[0]) print(len(word))
OCR ERL STRING FUNCTIONS — REFERENCE
OperationOCR ERLPython
Lengths.lengthlen(s)
Indexs[i]s[i]
Substrings.substring(a,n)s[a:a+n]
Uppercases.upper()s.upper()
Lowercases.lower()s.lower()
ASCIIASC() / CHR()ord() / chr()
⚡ In a 4-mark string program question: 1 mark input, 1 mark per string operation applied correctly. Show the operation name clearly and apply it to the right variable — those two things are what the mark scheme checks.
Common Mistakes

Common mistakes — avoid these in the exam

MISTAKE 1 — Off-by-one indexing
Using word[1] to get the first character. Strings are 0-indexed — the first character is always at index 0. Using index 1 returns the second character
✓ First character: word[0] — last character: word[word.length - 1]
MISTAKE 2 — Confusing substring() arguments
Writing word.substring(0, 4) expecting characters 0 to 4 — but the second argument is length, not an end index. substring(0, 4) returns 4 characters starting from 0
✓ OCR ERL: substring(start, length). Python slicing: [start:end] — these are NOT the same
MISTAKE 3 — Mixing types in concatenation
Writing "Length: " + word.length — this causes an error because length returns an integer and you cannot concatenate a string with an integer using +
✓ Always cast: "Length: " + str(word.length)
MISTAKE 4 — Thinking upper() changes the original
word.upper() does NOT change word — it returns a new string. After calling word.upper(), the variable word is still in its original case
✓ Store the result: upper ← word.upper() or use it directly in print
Summary

Key points — 2.2.1e

A string is a sequence of characters. Characters are stored at positions starting from index 0. Access with string[i]. Join strings using the + operator — this is concatenation
length — use myString.length or len(myString). The last character is always at index length − 1, not length. Remember: 0-indexed means one less than you might expect
substring(start, length) in OCR ERL extracts part of a string — second argument is how many characters. Python uses slicing [start:end] where end is exclusive (not included)
upper() and lower() return a new converted string — the original is unchanged. Use str(), int(), float() to convert between types. You cannot concatenate a string and an integer without casting
ASC() returns the ASCII code of a character — CHR() returns the character from a code. In Python: ord() and chr(). Key values: 'A'=65, 'a'=97, '0'=48
⚡ Next topic: 2.2.1f — Subroutines: Functions and Procedures. How to write and call reusable code blocks.
2.2.1e Complete

String Manipulation
Length · Index · Substring · Case

Get the full resource pack at CSZone.co.uk

📄
Marked Worksheet
CSZone.co.uk
Quiz
CSZone.co.uk
📊
Slides
CSZone.co.uk
Next Up
2.2.1f — Subroutines: Functions & Procedures