AQA · GCSE · Computer Science

    Course version: 8525 · Version from Sept 2020

    String handling operations in a programming language: substring

    A substring is a contiguous run of characters taken from a longer string, and extracting one is a core string-handling operation.

    Checked against the AQA 8525 specification (opens in a new tab) on .

    • Point 3 of 11

    Study notes

    From the checked teaching notes for this specification point.

    The idea

    A substring is a contiguous run of characters taken from a longer string, and extracting one is a core string-handling operation. In most languages you give a start position and a length, or a start and end position, and the language returns the characters in that range. Positions are usually counted from 0 in languages such as Python and from 1 in others, so the same request can select different characters. For example, in Python the expression word[2:5] on the string "COMPUTER" returns "MPU", because index 2 is the third character and the end index is excluded. Learners should be able to read a short code fragment, predict the substring produced, and choose the correct call for a stated goal.

    What earns the marks

    • Defines a substring as a contiguous sequence of characters taken from within a longer string, preserving their order.
    • Identifies that extraction needs a start position and either a length or an end position.
    • Recognises that indexing may start at 0 or 1 depending on the language, which changes the characters selected.
    • Predicts the exact characters returned by a given substring operation on a supplied string.
    • Selects the correct substring call that produces a stated target fragment.

    Common mistakes and how to put them right

    • Assuming every language counts the first character as position 1.

      Put it right: Check the language, since Python-style slicing starts at 0.

    • Treating the end index as inclusive in Python slicing.

      Put it right: The end index is excluded, so word[2:5] gives three characters.

    • Thinking a substring can skip characters or reorder them.

      Put it right: A substring is contiguous and keeps the original order.

    Exam tips

    • Write out the string with index numbers above each character before answering.
    • Count characters carefully and check whether the first index is 0 or 1 in the code shown.
    • Test your chosen option against the exact string in the question rather than a remembered example.

    Try the lesson

    The start of the String handling operations in a programming language: substring lesson, made from checked teaching notes for this specification point.

    Have a go first

    A string variable holds the value "COMPUTER". A substring operation is used to extract the characters from position 1 up to and including position 4, counting the first character as position 1. What is the result?

    There is more to this lesson

    The full lesson goes on to check your understanding and help you earn the marks. Create a free account to carry on, with your progress saved.

    Carry on with the full lesson

    A week of Premium from your first lesson · Then the free plan continues · No card, no charge

    Test your understanding of String handling operations in a programming language: substring

    Active recall beats re-reading. Take an instant quiz now to test what you’ve learned and lock it into long-term memory.

    Take topic quiz

    A week of Premium from your first lesson · Then the free plan continues · No card, no charge