AQA 7517, section 4.1.1.7
A string is a sequence of characters. Almost every program handles text, such as names, messages and input, so you need to know the standard string operations. The examples are in Python, where strings are indexed from 0.
Length
The length of a string is the number of characters in it, including spaces.
word = "computer"
print(len(word)) # 8
print(len("a b")) # 3 spaces count
Position
Each character in a string has a position (its index). Positions start from 0, so the first character is at position 0 and the last is at len(text) - 1. Some languages count from 1, so check which your exam uses.
Indexing gets the character at a given position:
text = "banana"
print(text[0]) # b the character at position 0
print(text[2]) # n
print(text[len(text) - 1]) # a the last character
Going the other way, the position operation finds where a character or a string first appears inside another. Python's find gives the position of the first match, or -1 if there is none:
print(text.find("n")) # 2
print(text.find("an")) # 1
print(text.find("x")) # -1 not found
Substring
A substring is a part of a string. In Python this is done with slicing, text[start:end], which includes start but stops before end.
text = "computer"
print(text[0:3]) # com
print(text[3:]) # puter from position 3 to the end
print(text[-1]) # r the last character
Concatenation
Concatenation joins strings together end to end.
first = "Ada"
last = "Lovelace"
full = first + " " + last
print(full) # Ada Lovelace
Character to character code and back
Every character is stored as a number, called its character code, from a character set such as ASCII or Unicode. You can convert in either direction.
print(ord("A")) # 65 character to code
print(chr(65)) # A code to character
print(chr(ord("a") - 32)) # A a simple lower to upper case shift
In ASCII, capital letters run from 65 to 90, lower case letters from 97 to 122, and digits from 48 to 57. Lower case letters have codes 32 higher than their capitals.
String conversion operations
Input from the keyboard always arrives as a string, so programs often need to convert between strings and other types. The specification expects these conversions:
| Conversion | Python | Example | Result |
|---|---|---|---|
| String to integer | int() |
int("42") |
42 |
| String to float | float() |
float("3.5") |
3.5 |
| Integer to string | str() |
str(42) |
"42" |
| Float to string | str() |
str(3.5) |
"3.5" |
| Date/time to string | strftime |
see below | "15/11/2026" |
| String to date/time | strptime |
see below | a date value |
from datetime import datetime
d = datetime.strptime("15/11/2026", "%d/%m/%Y") # string to date
print(d.year) # 2026
print(d.strftime("%d %B %Y")) # 15 November 2026
A conversion fails if the string doesn't hold a valid value. int("hello") raises an error, which is why programs often check input or use exception handling (covered in 4.1.1.9).
Watch out: "5" + "3" is "53", not 8. Joining two strings concatenates them, and adding needs numbers. Convert first: int("5") + int("3") is 8.
Worked example
Task: take a full name such as "Grace Hopper" and print the initials, "G.H.".
name = "Grace Hopper"
space = name.find(" ") # 5
first = name[0] # G
last = name[space + 1] # H
initials = first + "." + last + "."
print(initials) # G.H.
Steps: find the position of the space (5), take the character at position 0 for the first initial, and the character straight after the space (position 6) for the second. Then concatenate with the full stops.
Check yourself
What is len("hello world")?
11. The space counts as a character.
If s = "programming", what is s[3:7]?
"gram". The characters at positions 3, 4, 5 and 6 are g, r, a and m.
What is the result of "12" + "5", and how would you get 17?
The result is the string "125", because the strings are concatenated. To get 17, convert first: int("12") + int("5").
What does chr(ord("c") + 1) give?
"d". ord("c") is 99, adding 1 gives 100, and chr(100) is "d".