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))
lettercodeback
A

Output

 

Step 1: We start with a character: capital A, stored in letter. Nothing has been converted yet.

1 / 8

Watch the code column: it's the only one holding a number. That's what ord() hands you.

Step 1 of 8: We start with a character: capital A, stored in letter. Nothing has been converted yet..

Watch out: Capital A and lowercase a are different characters, so they get different codes: 65 and 97. Case matters.

Computer Science · The pattern in the codes

Where does each letter live?

You've seen A come out as 65 and a as 97. Each following capital letter is one more than the last. Drag every letter to its ASCII code.

Predict, then check

Work from the inside out. What does each part hand to the next?

What does print(chr(ord("B") + 1)) output?

Computer Science · What ASCII really is

Which of these do you believe?

Four students are arguing about what ASCII actually is.

Which idea is closest to what you think right now?
How sure are you?

Computer Science · Which way round?

Find the backwards line

Sam wants his program to output the ASCII code for D, then the character whose code is 66. One line is wrong. Which one?

Sam's program — which line goes wrong?

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

1ASCII gives every character in its set a unique numerical code, found in an ASCII table.
2A is 65, B 66, C 67, D 68; lowercase a is 97. Capital and lowercase letters have different codes.
3ord(character) returns the decimal code; chr(number) returns the character. Each undoes the other.
4ASCII is a fixed set used for a standard American or English keyboard, not every language's characters.

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?
A character set that gives each character a unique numerical value (its code).
Which characters is ASCII used to represent?
The characters on a standard American or English keyboard. It's a fixed, limited set, not every character in every language.
How do you find the code for a character you don't know?
Look it up in an ASCII table.
What is the ASCII code for capital A?
65 in decimal (41 in hexadecimal).
How do the codes change from A to B to C to D?
They go up by one for each following letter.
Do A and a have the same ASCII code?
No. They're different characters: A is 65, a is 97.
Python's ord(): what goes in, and what comes out?
A character goes in; its decimal ASCII value comes out.
Which Python function turns a decimal code into a character?
chr(). For example, chr(66) gives B.
What does chr(ord("A")) return, and why?
A. ord() turns A into 65 and chr() turns 65 back into A: the two functions undo each other.

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 posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

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.