String handling

A string is a variable that holds a sequence of one or more alphanumeric characters. These characters can be letters, numbers or symbols. It is usually possible to manipulate a string to provide information or to alter the contents of a string.

The following examples use strings:

wordOne = "Computer" wordTwo = "Science"


The length of a string can be determined using the pseudo-code LEN. This gives the length as an integer.

LEN(wordOne) - would give the answer "eight", as there are eight characters in the word "Computer"

Character position

It is possible to determine which character features at a position within a string:

wordOne[2] - would give the answer "m", as m is the third character in the word “Computer” (remember computers start counting at zero).


A substring is a string of characters that exists inside a string, for example, the first three characters of a password.

The code below extracts a substring of the first three letters of a string in two ways:

Code version one:

wordOne ← “Computer” sub ← “” FOR count ← 0 TO 2 sub ← sub + wordOne[count] ENDFOR OUTPUT sub

Code version two:

wordOne ← “Computer” OUTPUT SUBSTRING(0, 2, wordOne)

Both versions would give “Com”, the first three characters in the string. However, using the SUBSTRING function saves having to write as much code each time a substring is needed.

The SUBSTRING function takes three parameters:

  • an integer that indicates the starting point in the string
  • an integer that indicates the end point within the string
  • the string that the substring is to be taken from


Concatenation is the joining together of two strings . In the following program, the variable 'fullName', is created by joining the 'firstName' and 'lastName' values together with a space between the strings.

firstName ← “Bob” lastName ← “Smith” fullName ← firstName + " " + lastName OUTPUT fullName

This would output the name Bob Smith.

String conversion

Sometimes a programmer needs to change the data type stored within a variable. For example, an integer may need to be converted to a string in order to be displayed as part of a message. This process is known as casting. The following examples convert a string to an integer and an integer to a string using Python:

str(68) returns “68” int(“54”) returns 54

AQA pseudo-code uses the following functions:

  • STRING_TO_INT - string
  • STRING_TO_REAL - string
  • INT_TO_STRING - integer
  • REAL_TO_STRING - real
Casting allows a programmer to change the data type stored within a variable.

Character set conversions

Another way of dealing with data is to convert alphanumeric data into its numerical format in a character set. The pseudo-code CHAR_TO_CODE and CODE_TO_CHAR functions allow characters to be swapped between their text and number formats using the ASCII or UNICODE character set. Read more about these character sets in the data representation study guide.

CHAR_TO_CODE(character) - converts from alphanumeric character to ASCII/UNICODE character

CODE_TO_CHAR(integer) - converts from ASCII/UNICODE character to alphanumeric character

In the code below, the user enters a character. The code checks that this is between lower-case ‘a’ and ‘z’.

letter ← USERINPUT IF (CHAR_TO_CODE(character) >= CHAR_TO_CODE(“a”)) AND (CHAR_TO_CODE(character) <= CHAR_TO_CODE(“z”)) THEN OUTPUT “Character correct!” ELSE OUTPUT “Incorrect” ENDIF

In this example, CHAR_TO_CODE has been used to create a range check for the data.

In contrast, CODE_TO_CHAR takes an ASCII character and converts it to the alphanumeric character. This is useful when random letters need to be generated. For example, in the code below a random number is converted to a letter as part of a word guessing game.

letter ← RANDOM_INT(97,122) OUTPUT “does it contain” + CODE_TO_CHAR(character) + “?”