OCR J277 Topic 2.2

String Manipulation

Mastering Length, Upper, Lower, and the tricky Substring.

Learning Checklist

Your Progress:
0/8
Click to view full objective list
1

What is a String?

Exam Trap: The "Zero" Rule

Computers always start counting positions at 0, not 1. If you count from 1, you will lose marks on Substring questions.

The Necklace Analogy

Think of a String as a necklace of beads. Each bead is a character. Even a space counts as a bead!

P
0
y
1
t
2
h
3
o
4
n
5
2

The 3 Basic Tools

.length

Counts how many characters are in the string. Includes spaces and symbols.

msg = "Hi!"
print(msg.length)
// Output: 3

.upper

Converts all letters to UPPERCASE (Shouting).

name = "ben"
print(name.upper)
// Output: "BEN"

.lower

Converts all letters to lowercase (Whispering).

email = "USER"
print(email.lower)
// Output: "user"
3

The Boss Level: Substring

Snatching a piece of the string

Substring cuts out a specific part of the word. In OCR Reference Language, you need two numbers:

1

Start Position

Where do we start cutting? (Remember index 0!)

2

Length (OCR Only)

How many characters do we want to grab?

// Example: Grab "Put" from "Computer"

word = "COMPUTER"

part = word.substring(3, 3) // Start at 3 ('P'), take 3 letters

print(part) // Output: "PUT"

4

OCR Reference vs. Python

Critical Difference

In Python, we use "Slicing" [start:end]. In OCR Exams, we use "Substring" (start, length).
Do not mix these up in the exam!

Feature OCR Reference Language Python Code
Length string.length len(string)
Upper Case string.upper string.upper()
Lower Case string.lower string.lower()
Substring
(The Hard One)
string.substring(x, y)
Start at x, take y characters.
string[x:z]
Start at x, stop before index z.
5

Practice Question

Predict the Output

A variable is declared: username = "Neo_The_One"

1. print(username.length)

Show Answer

11

2. print(username.substring(0, 3))

Show Answer

"Neo"

3. print(username.upper)

Show Answer

"NEO_THE_ONE"

Key Vocabulary

String: A data type used for text (a collection of characters).
Index: The position of a character in a string. Starts at 0.
Concatenation: Joining two strings together (e.g. "Run" + "ner").
Reference Language: The pseudocode style used in OCR exams.

Lesson Summary & Review