GCSE · Computer Science · AQA · Spec 8525
Convert character to character code
In ASCII, the letter A has a number: 65. Not a typo. Let's ask Python for that number, then turn it straight back into the letter.
Computer Science · Character codes
Ask Python for the number
Step through eight lines. Watch the trace table: one column ends up holding a number, the others hold characters.
Before you press Next: line 2 runs ord(letter). Will code end up holding a letter or a number?
1 letter = "A"2 code = ord(letter)3 print(code)4 back = chr(code)5 print(back)6 print(ord("a"))7 print(chr(97))8 print(chr(66))
| letter | code | back |
|---|---|---|
| A |
Output
Step 1: We start with a character: capital A, stored in letter. Nothing has been converted yet.
Watch the code column: it's the only one holding a number. That's what ord() hands you.
Predict, then check
Work from the inside out. What does each part hand to the next?
What does print(chr(ord("B") + 1)) output?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Every letter you type has its own number hiding behind it. Learn to read those numbers, and to make Python swap a letter for its code and straight back again.
What you need to know
- ASCII is a character set: it gives each character its own unique number, called its code.
- It's used for the characters on a standard American or English keyboard, not every character in every language.
- To find a character's code, look it up in an ASCII table.
- Capital A is 65, and each following letter's code is one more than the last.
Have a goA is 65. Without looking anything up, what's the code for C?
67
C is two letters after A, so add two to 65. Answering 66 counts only one step: that's B.
- Lowercase a is a different character from capital A, so it has a different code: 97.
- These codes are decimal numbers. Written in hexadecimal, A to D are 41 to 44.
- In Python, ord() takes a character and returns its decimal ASCII code, so ord("A") gives 65.
- chr() works the other way: give it a decimal number and it returns the character, so chr(97) gives a.
Have a goYou've got the number 66 and you want a letter. Which function do you reach for, and what comes out?
chr(66), which gives B.
Number in, character out is chr()'s job. ord() goes the other way and wants a character, not a number.
- Run one after the other and they cancel out: chr(ord("A")) gives back A.
Have a goYour friend calls ord(chr(ord("D"))) 'a riddle with no answer'. Prove them wrong: what does it return?
68
The inner ord and chr undo each other, leaving ord("D"). D is three letters after A, so 65 + 3 = 68. It's a number, because the last function run is ord().
The big picture
ASCII is a character set that gives each character a unique number, its code, found in an ASCII table. It is used for the characters on a standard American or English keyboard, not every character in every language. Capital A is 65 and each following letter is one more: B 66, C 67, D 68. Lowercase a is a different character, with code 97. In Python, ord() turns a character into its decimal code and chr() turns a decimal number back into the character, so each undoes the other.
Key points
Worked example
Problem
A secret message arrives as three ASCII codes: 67, 65, 66. Which Python function turns each code into a character? Decode the message.
⚠ Watch out
Using the functions backwards. ord() needs a character and gives a number; chr() needs a number and gives a character, so ord(66) or chr("B") won't do what you want. The other classic slip: assuming lowercase a shares capital A's code. It doesn't: A is 65, a is 97.
Memory hook
ord: letter in, number out. chr: number in, CHaRacter out. Do one then the other and you're back where you started.
Check yourself
Cover the page. Write the Python call that turns the code 68 into its character, and say what it returns. Then explain in one sentence why ord("a") and ord("A") give different numbers.
Flashcards
(9)What is ASCII?
Which characters is ASCII used to represent?
How do you find the code for a character you don't know?
What is the ASCII code for capital A?
How do the codes change from A to B to C to D?
Do A and a have the same ASCII code?
Python's ord(): what goes in, and what comes out?
Which Python function turns a decimal code into a character?
What does chr(ord("A")) return, and why?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore AQA GCSE Computer Science topics
- Boolean data type
- Character data type
- Concept of a data type
- Concept of subroutines
- Constant declaration
- Convert character code to character
- Correcting errors
- Data validation routines
- Equal to (relational operator)
- Greater than (relational operator)
- Greater than or equal to (relational operator)
- Identifying and categorising errors
How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 4 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.