You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers string handling — the manipulation and processing of text data — and regular expressions — powerful pattern-matching tools used for searching, validating, and transforming strings. These are essential skills for A-Level Computer Science.
A string is a sequence of characters. In most programming languages, strings are immutable — once created, they cannot be changed in place. Any operation that appears to modify a string actually creates a new string.
| Operation | Description | Python Example | Result |
|---|---|---|---|
| Length | Number of characters | len("Hello") | 5 |
| Indexing | Access a single character | "Hello"[1] | "e" |
| Slicing | Extract a substring | "Hello"[1:4] | "ell" |
| Concatenation | Join two strings | "Hi" + " there" | "Hi there" |
| Repetition | Repeat a string | "Ha" * 3 | "HaHaHa" |
| Membership | Check if substring exists | "ell" in "Hello" | True |
| Upper/Lower | Change case | "Hello".upper() | "HELLO" |
| Strip | Remove whitespace | " Hi ".strip() | "Hi" |
| Split | Break into a list | "a,b,c".split(",") | ["a","b","c"] |
| Join | Combine a list into a string | ",".join(["a","b"]) | "a,b" |
| Replace | Replace occurrences | "cat".replace("c","b") | "bat" |
| Find | Find position of substring | "Hello".find("ll") | 2 |
You can iterate through every character in a string using a loop:
text = "COMPUTER"
FOR i = 0 TO LENGTH(text) - 1
OUTPUT text[i]
NEXT i
text = "COMPUTER"
for char in text:
print(char)
# With index
for i in range(len(text)):
print(f"Index {i}: {text[i]}")
Converting between strings and other data types is essential for input/output operations.
# String to integer
age = int("17")
# String to float
price = float("9.99")
# Integer to string
text = str(42)
# Character to ASCII code
code = ord("A") # 65
# ASCII code to character
char = chr(65) # "A"
| Character | ASCII Code | Character | ASCII Code |
|---|---|---|---|
| '0' | 48 | 'A' | 65 |
| '9' | 57 | 'Z' | 90 |
| ' ' | 32 | 'a' | 97 |
| '!' | 33 | 'z' | 122 |
Exam Tip: You should know the ASCII ranges for digits (48-57), uppercase letters (65-90), and lowercase letters (97-122). This knowledge is useful for character manipulation and simple encryption algorithms like Caesar cipher.
def is_valid_email(email: str) -> bool:
if email.count("@") != 1:
return False
local, domain = email.split("@")
if len(local) == 0 or len(domain) == 0:
return False
if "." not in domain:
return False
if domain.startswith(".") or domain.endswith("."):
return False
return True
def caesar_encrypt(text: str, shift: int) -> str:
result = ""
for char in text:
if char.isalpha():
base = ord('A') if char.isupper() else ord('a')
shifted = (ord(char) - base + shift) % 26 + base
result += chr(shifted)
else:
result += char
return result
print(caesar_encrypt("Hello World", 3)) # Khoor Zruog
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.