SLIDE 1
CSZone.co.uk
Click to advance · Arrow keys also work
Edexcel 1CP2 · Topic 6 · 6.5

String Operations &
Text Processing

ASCII · ord() · chr() · String Parsing · f-strings · format()

CSZoneEdexcel GCSE Computer Science 1CP2
ASCII & Character Codes

Characters as Numbers

print(ord("A")) # 65 (ASCII code)
print(ord("a")) # 97
print(ord("0")) # 48

print(chr(65)) # "A"
print(chr(97)) # "a"

# Convert uppercase to lowercase manually
char = "B"
lower = chr(ord(char) + 32)
print(lower) # b
ord(): character → ASCII integer code
chr(): ASCII integer → character
Uppercase A-Z: 65–90; Lowercase a-z: 97–122; Digits 0–9: 48–57
String Formatting

Producing Readable Output

name = "Alice"
score = 95

# f-string (modern, preferred)
print(f"Hello {name}, your score is {score}")

# format() method
print("Hello {}, your score is {}".format(name,score))

# Formatted numbers
price = 9.5
print(f"Price: £{price:.2f}") # £9.50
f-strings: prefix with f, embed variables in {} — most readable approach
:.2f: format float to 2 decimal places; :d: integer; :>10: right-align in 10 chars
String Parsing & Processing

Analysing Text Data

text = "Alice,Bob,Charlie,Diana"
names = text.split(",") # split by comma
print(names[0]) # Alice

# Count vowels in a word
word = "Python"
vowels = "aeiouAEIOU"
count = 0
for char in word:
if char in vowels:
count += 1
print(count) # 1
Parsing: breaking a string into parts — used for CSV data, user input processing
",".join(names): joins a list back into a string with a separator
Exam Practice

Have a go at this question

Edexcel-style question
Write Python code that takes a word from the user and prints how many letters are uppercase.
4 marks
word = input("Enter a word: ")
count = 0
for char in word:
if char.isupper():
count += 1
print("Uppercase letters:", count)
Key Takeaways

What to Remember

ord("A")=65, chr(65)="A" — convert between characters and ASCII codes
f-strings: f"Hello {name}" — cleanest way to embed variables in strings
split(): text → list; join(): list → text; isalpha(), isupper(), islower(), isdigit()
String iteration: for char in word — process each character individually