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 operations and array manipulation for the OCR A-Level Computer Science (H446) specification. These are essential programming skills that appear frequently in exam questions and practical programming tasks.
A string is a sequence of characters. Strings are immutable in Python -- once created, individual characters cannot be changed.
| Operation | Python | Pseudocode | Example |
|---|---|---|---|
| Length | len(s) | LENGTH(s) | len("Hello") returns 5 |
| Concatenation | s1 + s2 | s1 + s2 | "Hi" + " there" returns "Hi there" |
| Substring | s[start:end] | SUBSTRING(s, start, length) | "Hello"[1:4] returns "ell" |
| Character at index | s[i] | s[i] | "Hello"[0] returns "H" |
| Find/search | s.find(sub) | FIND(s, sub) | "Hello".find("ll") returns 2 |
| Upper case | s.upper() | UPPER(s) | "hello".upper() returns "HELLO" |
| Lower case | s.lower() | LOWER(s) | "HELLO".lower() returns "hello" |
| Strip whitespace | s.strip() | TRIM(s) | " hi ".strip() returns "hi" |
| Replace | s.replace(old, new) | -- | "cat".replace("c", "b") returns "bat" |
| Split | s.split(delim) | -- | "a,b,c".split(",") returns ["a", "b", "c"] |
# Character by character
text = "Hello"
for char in text:
print(char)
# With index
for i in range(len(text)):
print(f"Index {i}: {text[i]}")
Every character has a numeric ASCII code. Python provides functions to convert between characters and their codes:
| Function | Description | Example |
|---|---|---|
ord(c) | Character to ASCII code | ord("A") returns 65 |
chr(n) | ASCII code to character | chr(65) returns "A" |
| Characters | Range |
|---|---|
| Digits 0-9 | 48-57 |
| Uppercase A-Z | 65-90 |
| Lowercase a-z | 97-122 |
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
result += chr(base + shifted)
else:
result += char
return result
print(caesar_encrypt("Hello World", 3)) # Khoor Zruog
def count_vowels(text: str) -> int:
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
return count
print(count_vowels("Hello World")) # 3
text = "Hello123"
print(text.isalpha()) # False (contains digits)
print(text.isdigit()) # False (contains letters)
print(text.isalnum()) # True (all alphanumeric)
print(text.startswith("He")) # True
print(text.endswith("23")) # True
# Number to string
num = 42
text = str(num) # "42"
# String to number
text = "3.14"
num = float(text) # 3.14
integer = int("42") # 42
# String to list of characters
chars = list("Hello") # ['H', 'e', 'l', 'l', 'o']
# List to string
joined = "".join(chars) # "Hello"
csv_line = ",".join(["Alice", "17", "A"]) # "Alice,17,A"
An array is an ordered collection of elements, typically of the same type. In Python, arrays are implemented as lists.
# Creating arrays
numbers = [10, 20, 30, 40, 50]
names = ["Alice", "Bob", "Charlie"]
empty = []
# Accessing elements (0-indexed)
print(numbers[0]) # 10 (first element)
print(numbers[-1]) # 50 (last element)
print(numbers[1:3]) # [20, 30] (slice)
# Modifying elements
numbers[2] = 35 # [10, 20, 35, 40, 50]
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.