You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers string manipulation techniques as required by OCR J277 Section 2.3. String manipulation is the process of examining, modifying, and extracting parts of strings. OCR requires you to know specific string operations including length, substring, upper/lower case conversion, concatenation, and ASCII conversion.
A string is a sequence of characters. Each character in a string has a position (index), starting from index 0.
word = "COMPUTER"
// Index: 0=C, 1=O, 2=M, 3=P, 4=U, 5=T, 6=E, 7=R
The .length property returns the number of characters in a string.
OCR Pseudocode:
word = "Hello"
print(word.length) // Output: 5
Python:
word = "Hello"
print(len(word)) # Output: 5
Note: the length counts all characters including spaces and punctuation. "Hi there!".length = 9.
The .substring(start, end) method extracts a portion of a string from the start index up to (but not including) the end index.
OCR Pseudocode:
word = "COMPUTER"
print(word.substring(0, 3)) // Output: COM
print(word.substring(3, 6)) // Output: PUT
print(word.substring(4, 8)) // Output: UTER
Python:
word = "COMPUTER"
print(word[0:3]) # Output: COM
print(word[3:6]) # Output: PUT
print(word[4:8]) # Output: UTER
| Example | Start | End | Result | Characters Extracted |
|---|---|---|---|---|
"COMPUTER".substring(0, 3) | 0 | 3 | "COM" | Indices 0, 1, 2 |
"COMPUTER".substring(3, 6) | 3 | 6 | "PUT" | Indices 3, 4, 5 |
"COMPUTER".substring(6, 8) | 6 | 8 | "ER" | Indices 6, 7 |
OCR Exam Tip: The
.substring(start, end)method uses zero-based indexing and the end index is EXCLUSIVE (not included). This is a very common source of errors. For"HELLO".substring(1, 4), you get characters at indices 1, 2, 3 — which is "ELL", not "ELLO".
OCR Pseudocode:
word = "Hello World"
print(word.upper) // Output: HELLO WORLD
print(word.lower) // Output: hello world
Python:
word = "Hello World"
print(word.upper()) # Output: HELLO WORLD
print(word.lower()) # Output: hello world
userInput = input("Enter yes or no: ")
if userInput.lower == "yes" then
print("You chose yes")
endif
user_input = input("Enter yes or no: ")
if user_input.lower() == "yes":
print("You chose yes")
OCR Exam Tip: In OCR pseudocode,
.upperand.lowerdo NOT use brackets. In Python,.upper()and.lower()DO use brackets. Make sure you use the correct syntax for whichever language you are writing in.
Concatenation means joining two or more strings together using the + operator.
OCR Pseudocode:
firstName = "John"
lastName = "Smith"
fullName = firstName + " " + lastName
print(fullName) // Output: John Smith
Python:
first_name = "John"
last_name = "Smith"
full_name = first_name + " " + last_name
print(full_name) # Output: John Smith
When concatenating strings with numbers, convert the number to a string first:
age = 15
message = "You are " + str(age) + " years old"
print(message) // Output: You are 15 years old
Every character has a numerical ASCII code. OCR pseudocode provides ASC() and CHR() functions for conversion.
| Function | OCR Pseudocode | Python | Description |
|---|---|---|---|
| Character to ASCII code | ASC('A') | ord('A') | Returns the ASCII value: 65 |
| ASCII code to character | CHR(65) | chr(65) | Returns the character: 'A' |
| Character | ASCII Code |
|---|---|
| '0' to '9' | 48 to 57 |
| 'A' to 'Z' | 65 to 90 |
| 'a' to 'z' | 97 to 122 |
| Space | 32 |
OCR Pseudocode example — Caesar cipher shift:
letter = "H"
asciiValue = ASC(letter) // 72
shifted = asciiValue + 3 // 75
newLetter = CHR(shifted) // "K"
print(newLetter) // Output: K
Python:
letter = "H"
ascii_value = ord(letter) # 72
shifted = ascii_value + 3 # 75
new_letter = chr(shifted) # "K"
print(new_letter) # Output: K
The transformation flow that turns a single character into a shifted character can be drawn as a string manipulation pipeline:
flowchart LR
A[letter = 'H'] --> B[ASC letter]
B --> C[asciiValue = 72]
C --> D[asciiValue + 3]
D --> E[shifted = 75]
E --> F[CHR shifted]
F --> G[newLetter = 'K']
G --> H[print newLetter]
OCR Pseudocode:
original = input("Enter a word: ")
reversed = ""
for i = original.length - 1 to 0 step -1
reversed = reversed + original.substring(i, i + 1)
next i
print(reversed)
Python:
original = input("Enter a word: ")
reversed_str = ""
for i in range(len(original) - 1, -1, -1):
reversed_str += original[i]
print(reversed_str)
# Or more Pythonically:
print(original[::-1])
| Operation | OCR Pseudocode | Python |
|---|---|---|
| Length | str.length | len(str) |
| Substring | str.substring(start, end) | str[start:end] |
| Upper case | str.upper | str.upper() |
| Lower case | str.lower | str.lower() |
| Concatenation | str1 + str2 | str1 + str2 |
| Char to ASCII | ASC(char) | ord(char) |
| ASCII to char | CHR(code) | chr(code) |
OCR Exam Tip: String manipulation questions are very common on Paper 2. The most frequently tested operations are
.substring(),.length, andASC()/CHR(). Practise these until you can use them confidently and quickly.
The user enters their full name, and the program outputs the initials in upper case separated by dots (for example: "john samuel smith" becomes "J.S.S.").
OCR reference language pseudocode:
fullName = input("Enter your full name: ")
initials = ""
initials = initials + fullName.substring(0, 1).upper + "."
for i = 1 to fullName.length - 1
current = fullName.substring(i, i + 1)
previous = fullName.substring(i - 1, i)
if previous == " " AND current != " " then
initials = initials + current.upper + "."
endif
next i
print(initials)
Step-by-step trace for input "john samuel smith":
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.