You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
String manipulation means inspecting and transforming text data. Strings are one of the most commonly used data types, and being able to work with them is essential for GCSE Computer Science (AQA 3.2 / OCR J277 2.2).
A string is a sequence of characters. Each character has a position (index), starting from 0:
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Char | H | e | l | l | o | W | o | r | l | d | ! |
greeting = "Hello World!"
print(greeting[0]) # H
print(greeting[6]) # W
print(greeting[11]) # !
To find the number of characters in a string:
LEN("Hello") returns 5len("Hello") returns 5Note: Spaces, digits, and punctuation all count as characters.
A substring is a portion of a string. You extract substrings using slicing.
word ← "Computer"
OUTPUT SUBSTRING(3, 4, word)
# Extracts 4 characters starting from position 3 → "pute"
AQA format: SUBSTRING(startPosition, numberOfCharacters, stringName)
word = "Computer"
print(word[3:7]) # "pute" (index 3, 4, 5, 6)
print(word[:4]) # "Comp" (first 4 characters)
print(word[4:]) # "uter" (from index 4 to the end)
print(word[-3:]) # "ter" (last 3 characters)
Exam Tip: Remember that Python slicing uses
[start:stop]wherestopis exclusive — the character at thestopindex is not included.
Concatenation means joining strings together using the + operator:
first = "Hello"
second = "World"
combined = first + " " + second
print(combined) # "Hello World"
In pseudocode, concatenation uses + as well.
| Operation | Pseudocode | Python | Example |
|---|---|---|---|
| Uppercase | UPPER("hello") | "hello".upper() | "HELLO" |
| Lowercase | LOWER("HELLO") | "HELLO".lower() | "hello" |
This is useful for case-insensitive comparisons:
user_input = input("Enter yes or no: ")
if user_input.lower() == "yes":
print("You chose yes")
Every character has a numeric ASCII (American Standard Code for Information Interchange) value:
| Character | ASCII Value |
|---|---|
| 'A' | 65 |
| 'Z' | 90 |
| 'a' | 97 |
| 'z' | 122 |
| '0' | 48 |
| '9' | 57 |
ASC("A") returns 65; CHR(65) returns "A"ord("A") returns 65; chr(65) returns "A"print(ord("A")) # 65
print(chr(97)) # "a"
flowchart LR
A[String value] --> B[LEN length]
A --> C[SUBSTRING slice]
A --> D[UPPER / LOWER case]
A --> E[Concatenate with +]
A --> F[ASC / CHR ASCII]
A --> G[split into list]
A --> H[replace text]
A --> I[find position]
"hello".isalpha() # True (all letters)
"12345".isdigit() # True (all digits)
"hello".isalnum() # True (letters or digits)
sentence = "the cat sat on the mat"
words = sentence.split(" ")
print(words) # ["the", "cat", "sat", "on", "the", "mat"]
print(len(words)) # 6
text = " hello "
print(text.strip()) # "hello"
print(text.lstrip()) # "hello "
print(text.rstrip()) # " hello"
email = "user@example.com"
position = email.find("@")
print(position) # 4
message = "Hello World"
new_message = message.replace("World", "Python")
print(new_message) # "Hello Python"
word = "GCSE"
for char in word:
print(char)
Or using indices:
for i in range(len(word)):
print(f"Index {i}: {word[i]}")
text = "Computer Science"
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
print(f"Number of vowels: {count}")
word[0] = "X". Instead, create a new string."Yes" is not equal to "yes".LEN returns the number of characters.+) joins strings.UPPER and LOWER convert case.ASC / CHR convert between characters and ASCII codes.A palindrome reads the same forwards and backwards.
def is_palindrome(text):
text = text.lower()
reversed_text = ""
for i in range(len(text) - 1, -1, -1):
reversed_text = reversed_text + text[i]
return text == reversed_text
print(is_palindrome("Racecar")) # True
print(is_palindrome("Hello")) # False
Trace for "Racecar":
| Step | text | reversed_text after loop |
|---|---|---|
| lowered | "racecar" | "racecar" |
Result: True.
def shift_letter(letter, shift):
base = ord('A') if letter.isupper() else ord('a')
offset = (ord(letter) - base + shift) % 26
return chr(base + offset)
def caesar(text, shift):
result = ""
for ch in text:
if ch.isalpha():
result = result + shift_letter(ch, shift)
else:
result = result + ch
return result
print(caesar("Hello, World!", 3)) # Khoor, Zruog!
print(caesar("Khoor, Zruog!", -3)) # Hello, World!
text = input("Enter a sentence: ").lower()
vowels = ["a", "e", "i", "o", "u"]
counts = [0, 0, 0, 0, 0]
for ch in text:
for i in range(len(vowels)):
if ch == vowels[i]:
counts[i] = counts[i] + 1
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.