Strings and String Manipulation at KS3 Computing

A string is a sequence of characters — letters, digits, spaces, and symbols — stored together as a single piece of text data. Strings appear in virtually every program you write, from storing a player's name to displaying a message on screen. At KS3, learning to manipulate strings opens up powerful real-world programming techniques.

What is a string and how do you create one?

In Python, a string is created by enclosing text in either single quotes or double quotes:

greeting = "Hello, world!"
username = 'professor_turing'
empty_string = ""
digit_string = "42" # This is text, not a number

The key distinction is that 42 (an integer) and "42" (a string) are fundamentally different types. You can do arithmetic with 42; you cannot directly add it to other strings or use it in calculations without casting.

Strings in Python are immutable — once created, the characters inside cannot be changed individually. You can, however, create a new string based on an existing one.

How does string concatenation work?

Concatenation means joining strings together. In Python, the + operator concatenates two strings:

first_name = "Alan"
last_name = "Turing"
full_name = first_name + " " + last_name # "Alan Turing"
print(full_name)

You can also repeat a string using *:

border = "=" * 30
print(border) # ==============================

One important rule: you can only concatenate strings with other strings. Attempting to concatenate a string with an integer raises a TypeError:

age = 16
message = "You are " + age + " years old." # TypeError!
message = "You are " + str(age) + " years old." # Correct

A cleaner alternative to concatenation for longer strings is an f-string (formatted string literal):

age = 16
message = f"You are {age} years old." # Python 3.6+
print(message) # You are 16 years old.

How does string indexing work?

Every character in a string has a position called an index. Python uses zero-based indexing — the first character is at index 0, not index 1.

word = "COMPUTING"
# C O M P U T I N G
# Index: 0 1 2 3 4 5 6 7 8

You access a character using square brackets:

print(word[0]) # C
print(word[4]) # U
print(word[-1]) # G (negative index counts from the end)

Slicing extracts a portion of a string. The syntax is string[start:stop], where start is included and stop is excluded:

word = "COMPUTING"
print(word[0:4]) # COMP (indices 0, 1, 2, 3)
print(word[3:7]) # PUTI (indices 3, 4, 5, 6)
print(word[:3]) # COM (from start to index 2)
print(word[6:]) # ING (from index 6 to end)

What are the most useful string methods in Python?

A method is a function that belongs to a particular type. String methods are called using dot notation: string.method(). Here are the most important ones at KS3:

Method | What it does | Example | Result
len(s) | Returns the number of characters (note: a function, not a method) | len("Hello") | 5
s.upper() | Returns the string in all upper case | "hello".upper() | "HELLO"
s.lower() | Returns the string in all lower case | "HELLO".lower() | "hello"
s.strip() | Removes leading and trailing whitespace | " hi ".strip() | "hi"
s.replace(old, new) | Replaces all occurrences of old with new | "cat".replace("c","b") | "bat"
s.find(sub) | Returns the index of the first occurrence of sub, or -1 | "hello".find("l") | 2
s.count(sub) | Counts how many times sub appears | "banana".count("a") | 3

name = " professor turing "
name = name.strip() # "professor turing"
name = name.title() # "Professor Turing" (capitalises first letter of each word)
print(name)

A worked example: username validator

The following program uses string methods to check whether a proposed username meets a set of rules:

def validate_username(username):
username = username.strip()

if len(username) < 4:
return "Too short — must be at least 4 characters."
if len(username) > 20:
return "Too long — must be 20 characters or fewer."
if " " in username:
return "No spaces allowed in a username."
return "Valid username: " + username.lower()

print(validate_username("Al")) # Too short
print(validate_username("professor turing")) # No spaces
print(validate_username("Turing42")) # Valid username: turing42

This example uses len(), .strip(), the in operator (which checks whether a substring appears inside a string), and .lower(). All of these are standard KS3 programming skills.

How do strings relate to other KS3 topics?

Strings connect to several other areas of the curriculum:

Data representation — strings are stored in memory as sequences of character codes. In ASCII and Unicode, each character maps to a number (e.g. 'A' = 65).

Variables and data types — strings are one of the four core data types alongside integers, floats, and Booleans.

Algorithms — searching a string for a pattern is itself a classic algorithmic problem.

Cyber security — password validation, input sanitisation, and preventing injection attacks all rely on string processing.

The DfE curriculum asks students to understand how data is represented and manipulated in programs (gov.uk/government/publications/national-curriculum-in-england-computing-programmes-of-study). Strings are the most immediate, everyday example of this.

Frequently asked questions

What is a string in programming at KS3?

A string is a sequence of characters treated as a single piece of text data. In Python it is created by enclosing text in quotes (single or double). Strings can contain letters, digits, spaces, and symbols. The key fact is that even if a string contains digits (e.g. "42"), it is still text — you cannot use it in arithmetic without first converting it to an integer or float using int() or float().

How do you concatenate two strings in Python?

Use the + operator: "Hello" + " " + "World" produces "Hello World". Both values being joined must be strings. If one is a number, convert it first with str(). Alternatively, use an f-string: f"You are {age} years old." which inserts the value of age automatically without needing explicit casting.

What is string indexing and why does Python start at 0?

String indexing assigns each character a position number called an index. Python (like most programming languages) starts counting from 0, so the first character is at index 0, the second at index 1, and so on. The last character is at index len(string) - 1, or you can use -1 as a shortcut. Slicing uses the same system: word[1:4] returns characters at indices 1, 2, and 3 (the stop index is excluded).

What does len() do in Python?

len() is a built-in Python function that returns the number of items in an object. For a string, it returns the total number of characters (including spaces and punctuation). For example, len("Hello!") returns 6. It is one of the most frequently used functions in KS3 programming, appearing in validation routines, loop conditions, and wherever the length of a piece of text matters.

For Socratic computing tutoring at KS3 and GCSE — see aitutors.me.