String manipulation

GCSE Computer Science (OCR) · OCR J277 · Algorithms and programming (Paper 2) › Programming fundamentals › Additional programming techniques

66 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.

Or take a quick quiz: 10 questions on this subtopic in GCSE Computer Science (OCR), marked as you tap.

Revision notes

String manipulation means working with text: finding its length, taking part of it, changing its case, joining strings and converting between characters and their ASCII codes. Expect 'state the result' questions and short programs that loop through the characters of a string.

Key facts

  • word.length: number of characters
  • word.substring(start, n): n characters from position start (first position = 0)
  • word.left(n) / word.right(n): first / last n characters
  • word.upper / word.lower: change the case
  • ASC("A") = 65; CHR(65) = "A"; ASC("a") = 97
  • Concatenate with +; cast numbers with str() first
  • Every character: for i = 0 to word.length - 1 … word.substring(i, 1)

Notes

String operations in ERL

  • word.length: the number of characters in the string.
  • word.substring(start, n): n characters, starting at position start. The first character is at position 0. Taking part of a string like this is called slicing.
  • word.left(n) and word.right(n): the first n or last n characters.
  • word.upper and word.lower: the string in upper or lower case.
  • Concatenation: join strings with +. Cast numbers first: "Level " + str(3) gives "Level 3".

Worked example: word = "Keyboard"

diagram
  • Positions: K is 0, e is 1, y is 2, b is 3, o is 4, a is 5, r is 6, d is 7.
  • Keyboard01234567positionsubstring(3, 4) = "boar"left(3) = "Key"right(5) = "board"
    Positions start at 0: substring(3, 4) takes 4 characters starting at position 3
  • word.length gives 8.
  • word.substring(3, 4) gives "boar" (4 characters from position 3).
  • word.left(3) gives "Key" and word.right(5) gives "board".
  • word.upper gives "KEYBOARD".

Character codes

  • ASC(ch) gives the character code (ASCII) of a character: ASC("A") is 65 and ASC("a") is 97.
  • CHR(code) gives the character for a code: CHR(66) is "B".
  • Letters have consecutive codes, so CHR(ASC("C") + 2) gives "E".

Looping through a string

  • To check every character, loop from 0 to length − 1 and take one character at a time:
    spaces = 0
    for i = 0 to sentence.length - 1
       if sentence.substring(i, 1) == " " then
          spaces = spaces + 1
       endif
    next i

How to answer each type of question

State the result of string operations

1 mark eachGrade 5
  1. Write the string out and number the positions from 0.
  2. For substring, start at the given position and count n characters.
  3. Give text results in quotation marks and numbers without them.

Example. city = "Manchester"
State the result of:
(a) city.length (b) city.substring(3, 4) (c) city.upper (d) city.right(4) (e) CHR(ASC("C") + 2) [5]

Show the model answerHide the model answer
(a) 10
(1)
(b) "ches"
(1)
(c) "MANCHESTER"
(1)
(d) "ster"
(1)
(e) "E"
(1)

Write code that builds a string

3 to 4 marksGrade 6
  1. Input the data you need.
  2. Use substring, left or right to take the part you need.
  3. Change the case if asked, then join the parts with +.
  4. Output (or return) the result.

Example. A username is the first three letters of a surname in upper case, followed by the year of birth. For example, Patel born in 2009 has the username PAT2009.
Write a program that inputs a surname and a year of birth and outputs the username. [4]

Show the model answerHide the model answer
surname = input("Enter surname")
year = input("Enter year of birth")
first3 = surname.left(3)
username = first3.upper + year
print(username)
Inputs the surname and the year (1); takes the first three characters, using left(3) or substring(0, 3) (1); converts them to upper case (1); joins them to the year and outputs the result (1).

Write a function that loops through a string

4 to 5 marksGrade 8
  1. Set a counter to 0.
  2. Loop from 0 to length − 1, taking one character each time.
  3. Test the character, using OR for several possible matches.
  4. Return (or output) the result after the loop.

Example. Write a function countVowels(word) that returns the number of vowels (a, e, i, o, u) in word. The word is in lower case. [5]

Show the model answerHide the model answer
function countVowels(word)
   count = 0
   for i = 0 to word.length - 1
      letter = word.substring(i, 1)
      if letter == "a" OR letter == "e" OR
         letter == "i" OR letter == "o" OR
         letter == "u" then
         count = count + 1
      endif
   next i
   return count
endfunction
Loops through every character, 0 to length − 1 (1); takes one character at a time (1); checks it against all five vowels with OR (1); adds 1 to a count set to 0 before the loop (1); returns the count after the loop (1).

Don’t lose marks

  • Counting positions from 1 instead of 0.
  • Treating the second value in substring as an end position.
  • Looping to word.length instead of word.length − 1, which goes past the last character.
  • Joining a number to a string without casting it with str().
  • Writing letter == "a" OR "e" instead of repeating the comparison each time.

More tips

Memory tricks

  • Positions start at 0, so the last character is at position length − 1.
  • substring(start, n): the second number is HOW MANY characters, not where to stop.
  • You only need two codes: A = 65 and a = 97. The other letters follow in order, so B = 66, C = 67, b = 98 and so on.

Exam technique

  • Write the string out with its position numbers underneath before answering.
  • If the question defines how a string function works, use that definition exactly.
  • Use the variable and function names given in the question.

What each grade needs

What you need to be able to do, from the first marks up to the top grade.

  1. Grade 4
    Join strings together (concatenation)Use + to join strings, casting numbers with str() first.
  2. Grade 5
    Use length, upper and lowerword.length gives the number of characters; .upper and .lower change the case.
  3. Grade 5
    Extract part of a stringUse .substring(start, n), .left(n) and .right(n), counting positions from 0.
  4. Grade 6
    Use ASC and CHRASC("A") gives the character code 65; CHR(66) gives "B".
  5. Grade 7
    Loop through each character of a stringUse for i = 0 to word.length - 1 with word.substring(i, 1).
  6. Grade 8
    Write functions that process stringsFor example count vowels, check a password's characters or build a code.

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

A program contains the line word = "Keyboard". In this language text.substring(start, n) returns n characters of text starting at position start, and the first character is at position 0. State the value of word.length.
8
Look at this program.
01 first = "Ada"
02 last = "Lovelace"
03 full = first + " " + last
04 print(full)
05 print(full.length)
State the output of line 04.
Ada Lovelace
Name the string handling operation that joins one string onto the end of another string.
Concatenation
State the position number of the first character in a string.
0
Complete the sentence. The ________ function converts a character into its ASCII code.
ASC
A program must check that a password is long enough. State the string operation that finds how many characters the password has.
length

Sample questions

Written for this site in the style of OCR exam questions. They are not taken from real past papers.

Question 1Easy4 marks
A program contains the line word = "Keyboard". In this language text.substring(start, n) returns n characters of text starting at position start, and the first character is at position 0.
(a) State the value of word.length.
[1]
(b) State the value of word.upper.
[1]
(c) State the value of word.substring(0, 3).
[1]
(d) State the value of word.substring(3, 5).
[1]
Show the answer and mark scheme
(a) Answer: 8
  • 8
(b) Answer: KEYBOARD
  • KEYBOARD (all capitals)
(c) Answer: Key
  • Key
(d) Answer: board
  • board
Question 2Medium7 marks
A school creates each pupil's username from:
  • the first three letters of their surname
  • the first letter of their first name
  • the last two digits of the year they joined.
The username is in upper case. For example, Mia Patel, who joined in 2025, has the username PATM25. In this language text.substring(start, n) returns n characters of text starting at position start, and the first character is at position 0.
(a) Write an algorithm that asks the user to enter a pupil’s first name, surname and year of joining (as a 4-character string), and outputs the pupil’s username.
You must use either OCR Exam Reference Language or a high-level programming language that you have studied.
[5]
(b) State the username for Jo Lin, who joined in 2024.
[1]
(c) Explain a problem that could happen if a pupil’s surname has only two letters, for example Ng.
[1]
Show the answer and mark scheme
(a) Answer:
first = input("First name")
surname = input("Surname")
year = input("Year joined")
user = surname.substring(0, 3) + first.substring(0, 1)
user = user + year.substring(2, 2)
print(user.upper)
  • inputs the first name, surname and year
  • extracts the first three letters of the surname (substring(0, 3))
  • extracts the first letter of the first name (substring(0, 1))
  • extracts the last two digits of the year (substring(2, 2))
  • joins the parts in the correct order, converts to upper case and outputs the username
(b) Answer: LINJ24
  • LINJ24
(c)
  • substring(0, 3) asks for more characters than the surname has, so an error could occur / the username would be shorter than expected
Question 3Hard7 marks
A password is valid if it:
  • is at least 8 characters long
  • contains at least one digit (0 to 9)
  • contains at least one upper-case letter (A to Z).
The ASCII codes for the digits 0 to 9 are 48 to 57, and for the letters A to Z are 65 to 90. In this language text.substring(start, n) returns n characters of text starting at position start, and the first character is at position 0.
(a) Write a function that takes a password as a parameter and returns True if the password is valid, or False if it is not.
You must use either OCR Exam Reference Language or a high-level programming language that you have studied.
[6]
(b) Give an example of a password that is at least 8 characters long but is not valid, and state why it is not valid.
[1]
Show the answer and mark scheme
(a) Answer:
function isValid(pw)
   hasDigit = false
   hasUpper = false
   for i = 0 to pw.length - 1
      code = ASC(pw.substring(i, 1))
      if code >= 48 AND code <= 57 then
         hasDigit = true
      elseif code >= 65 AND code <= 90 then
         hasUpper = true
      endif
   next i
   return pw.length >= 8 AND hasDigit AND hasUpper
endfunction
  • checks that the length is at least 8
  • loops through every character of the password
  • uses ASC to find the code of each character
  • records (flag / counter) when a code is from 48 to 57
  • records (flag / counter) when a code is from 65 to 90
  • returns True only when all three conditions are met, otherwise False
(b)
  • a suitable example with its reason, e.g. "sunshine7" has no upper-case letter / "Sunshines" has no digit

More in Additional programming techniques

Stuck? Get 1-to-1 help.

Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.