GGCSE Revision Hub

3.2.8 String handling operations in a programming language

AQA Computer Science 8525Topic 3.2 — ProgrammingFoundation & Higher

This is the longest topic in the specification and the one that carries most of the Paper 1 marks. It covers the statements every program is built from, the operators that do the work, the structures that hold data, and the discipline that makes code safe to rely on. Two of the specification's supported languages are Python and C#, so the examples here use Python-style syntax with the AQA pseudo-code where the specification demands it. Learn this as something you write, not something you memorise.

What you need to know

Why strings need operations

Text is everywhere in programs: names, addresses, messages, file names, and the answers to questions the program asks. A string is a sequence of characters, and the operations below are the ones you need to work with it. Recognising which operation a line of code performs is a very common exam question.

Key fact: Strings are sequences of characters, and these are the operations that work on them.

Length and position

Length gives you the number of characters in a string, and position gives you the index of a particular character or the starting index of a substring. Position in these operations is usually the same as array indexing: the first character is at position 0. Length and position together are what you need to loop through a string one character at a time.

Key fact: Length counts the characters; position finds a character or the start of a substring, counting from 0.

Substring and concatenation

Substring extracts part of a string — given a start position and a length, or a start and an end. Concatenation joins two strings into one, with no space inserted between them unless you put one there yourself. Concatenation is also how the program builds up a message one piece at a time, which is why it appears so often in exam code.

Key fact: Substring extracts part of a string; concatenation joins two strings with no separator.

Character codes

Convert character to character code converts one symbol into the number that represents it — so 'A' becomes 65. Convert character code to character does the reverse, turning 65 back into 'A'. These two operations are the bridge between text and numbers, and they are what lets a program sort characters alphabetically by comparing their codes rather than the letters themselves.

Key fact: Character to code and code to character are inverses; codes let you compare and sort text.

String conversions

The expected string conversion operations are: string to integer, string to real, integer to string, and real to string. These are what you use to make user input usable — string to integer turns '42' into 42 — and to make numbers displayable — integer to string turns 42 into '42' so it can be joined to other text.

Key fact: Four conversions: string to integer, string to real, integer to string, real to string.

Which one is needed

If the question tells you the data will be a number, use string to integer or string to real. If the number has decimal places, string to real, because string to integer would throw away the decimal part. If you are joining a number to some text, convert it to a string first, or the join will fail.

Key fact: Use string to real for decimal numbers, and convert numbers to strings before joining them to text.

Flashcards

Exam questions

Written in the style of AQA Computer Science, with a mark scheme for each one. No question here is a real past paper question.

mcq1 mark

Which string operation would you use to find out how many characters are in a name?

  • Length
  • Concatenation
  • Substring
  • Position

Mark scheme: 1 mark for identifying length, which returns the number of characters in a string. Concatenation joins strings, substring extracts part of one and position finds a character or the start of a substring.

short3 marks

Explain in English how you would convert a number typed at the keyboard into a value you can use in arithmetic, and what happens if you use the wrong conversion.

Mark scheme: 1 mark for identifying that the input must be converted with string to integer, or string to real if it has decimal places. 1 mark for explaining that without conversion the value stays a string. 1 mark for explaining the consequence, that arithmetic on a string concatenates instead, so '5' + '5' gives '55' rather than 10.

six6 marks

Explain in English the string handling operations in the specification and where you would use them in a program.

Mark scheme: Level 1 (1-2 marks): A short answer naming length and concatenation. Level 2 (3-4 marks): A structured answer covering the operations with examples. Level 3 (5-6 marks): A detailed answer describing length, position, substring and concatenation, explaining that character codes are numeric representations of symbols and that the two conversion operations are inverses, explaining the four string conversion operations and why string to integer versus string to real matters, and showing where each operation would be used in practice, such as validating a name's length, extracting a surname, or joining a formatted total into a message.

Other sections in this topic

Practise these questions

Answer them in the interactive view and they get marked against the same mark scheme, one mark at a time, with the reason for each mark.

Open the interactive topic →