← ACARA Digital Technologies Years 9–10

ord() and chr() in Python

ord("A") returns the number code of a character (65). chr(65) turns a number code back into its character ("A"). Ciphers use them to change letters to numbers and back.

Understanding ord() and chr() in Python in ACARA Digital Technologies Years 9–10

What it does: ord("D") is 68 and ord("A") is 65, so the result is 3, the position of D in the alphabet starting from 0. 2 + 65 is 67, and chr(67) is "C", so the position 2 becomes the letter C. ord() turns a character into its number code. The code for capital A is 65. Think of ord as 'order number' of a character. chr goes the other way, from number to character. chr() takes a number and gives a character. ord() needs a character, so ord(72) causes an error. A is 65, so subtracting 65 makes A = 0, B = 1 and D = 3.

Common mistakes: chr() turns a number into a character, so it cannot take the letter "A". To get the number code of a letter, use ord(letter). ord() only accepts a single character. Passing the whole string "HI" causes a TypeError. It should be ord(first), which is the single letter "H".

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 860–875) and word search (Modules 860–944).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
What does ord("A") return in Python?
65"A"
Matches
ord(65)
↓
ord() needs a character such as "A", not a number. To turn 65 into a letter, use chr(65).
Applying
Meanings
Term
ord() and chr()
Meaning
ord("A") returns the number code of a character (65). chr(65) turns a number code back into its character ("A"). Ciphers use them to change letters to numbers and back.
Practice
letter = "D"
Testing
Gaps
print(("A"))
Check Answer
Spots
1letter = "A"
2code = chr(letter)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Scorpion' badge — Venom Debugger, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. What does ord("A") return in Python?
A. 65 ✓
B. "A"
ord() turns a character into its number code. The code for capital A is 65.
2. What does chr(66) return?
A. "B" ✓
B. 66
chr() turns a number code back into a character. Code 66 is capital B.
3. Which function changes a letter into a number?
A. ord() ✓
B. chr()
Think of ord as 'order number' of a character. chr goes the other way, from number to character.
4. Which function changes the number 72 into the letter "H"?
A. ord(72)
B. chr(72) ✓
chr() takes a number and gives a character. ord() needs a character, so ord(72) causes an error.
5. Which line gives the position 0-25 of the letter "D" in the alphabet?
A. ord("D") + 65
B. ord("D") - 65 ✓
A is 65, so subtracting 65 makes A = 0, B = 1 and D = 3.

Match the Pairs

ord("A") → Returns the number code of the character A, which is 65
chr(66) → Returns the character for the number code 66, which is "B"
Convert a letter into a position from 0 to 25 by taking away the code of "A" → ord("D") - ord("A")
Turn a position from 0 to 25 back into an uppercase letter by adding the code of "A" and using chr() → chr(2 + ord("A"))
ord(65) → ord() needs a character such as "A", not a number. To turn 65 into a letter, use chr(65).
chr("A") → chr() needs a number code such as 65, not a letter. To get the code of "A", use ord("A").

Key Term

ord() and chr()
ord("A") returns the number code of a character (65). chr(65) turns a number code back into its character ("A"). Ciphers use them to change letters to numbers and back.

Fill in the Blanks

1. print(ord("A"))
Hint: Type the function that turns a character into its number code.
2. print(chr(65))
Hint: Type the function that turns a number code back into a character.
3. pos = ord("D") - 65 print(pos)
Hint: Type the number code of the first letter of the alphabet, so the position starts at 0.
4. letter = "C" num = ord(letter) - 65 print(num)
Hint: Type the variable name that holds the 0-25 position of the letter.
5. print(chr(ord("A") + 1))
Hint: Type the operator that moves the code up to the next letter.

Spot the Error

Question 1
A letter = "A"
B code = chr(letter)
C print(code)
Line B — chr() turns a number into a character, so it cannot take the letter "A". To get the number code of a letter, use ord(letter).
Question 2
A word = "HI"
B first = word[0]
C code = ord(word)
D print(code)
Line C — ord() only accepts a single character. Passing the whole string "HI" causes a TypeError. It should be ord(first), which is the single letter "H".
Question 3
A pos = ord("C") + 65
B print(pos)
C print(chr(pos + 65))
Line A — To turn a letter into a position from 0 to 25, subtract 65 (the code for "A"). Adding 65 gives 132 instead of 2. It should be ord("C") - 65.
Question 4
A pos = 5
B code = pos + 65
C letter = ord(code)
D print(letter)
Line C — ord() changes a character into a number, but code is already a number. To change a number code into a letter you need chr(code).
Question 5
A letter = "Z"
B pos = ord(letter) - 64
C print(pos)
Line B — "A" has code 65, so subtract 65 to start counting at 0. Subtracting 64 makes Z equal 26 instead of 25. It should be - 65.

Student with a class code? Join your class →