ord() and chr() in Python

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

ord() and chr() in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does ord("A") return in Python?
A 65
B "A"
2. What does chr(66) return?
A "B"
B 66
3. Which function changes a letter into a number?
A ord()
B chr()
4. Which function changes the number 72 into the letter "H"?
A ord(72)
B chr(72)
5. Which line gives the position 0-25 of the letter "D" in the alphabet?
A ord("D") + 65
B ord("D") - 65

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1ord("A") Achr(2 + ord("A"))
2chr(66) Bord("D") - ord("A")
3Convert a letter into a position from 0 to 25 by taking away the code of "A" Cord() needs a character such as "A", not a number. To turn 65 into a letter, use chr(65).
4Turn a position from 0 to 25 back into an uppercase letter by adding the code of "A" and using chr() Dchr() needs a number code such as 65, not a letter. To get the code of "A", use ord("A").
5ord(65) EReturns the character for the number code 66, which is "B"
6chr("A") FReturns the number code of the character A, which is 65

Definitions

ord() and chr()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
print(___("A"))
2.
print(___(65))
3.
pos = ord("D") - __ print(pos)
4.
letter = "C" num = ord(letter) - 65 print(___)
5.
print(chr(ord("A") _ 1))

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A letter = "A"
B code = chr(letter)
C print(code)
Question 2
A word = "HI"
B first = word[0]
C code = ord(word)
D print(code)
Question 3
A pos = ord("C") + 65
B print(pos)
C print(chr(pos + 65))
Question 4
A pos = 5
B code = pos + 65
C letter = ord(code)
D print(letter)
Question 5
A letter = "Z"
B pos = ord(letter) - 64
C print(pos)

Answer Key

ord() and chr() in Python
Section A: Quick Questions
Q1
A — 65
Q2
A — "B"
Q3
A — ord()
Q4
B — chr(72)
Q5
B — ord("D") - 65
Section B: Match the Pairs
Q1
ord("A") → Returns the number code of the character A, which is 65
Q2
chr(66) → Returns the character for the number code 66, which is "B"
Q3
Convert a letter into a position from 0 to 25 by taking away the code of "A" → ord("D") - ord("A")
Q4
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"))
Q5
ord(65) → ord() needs a character such as "A", not a number. To turn 65 into a letter, use chr(65).
Q6
chr("A") → chr() needs a number code such as 65, not a letter. To get the code of "A", use ord("A").
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
print(ord("A"))
Q2
print(chr(65))
Q3
pos = ord("D") - 65 print(pos)
Q4
letter = "C" num = ord(letter) - 65 print(num)
Q5
print(chr(ord("A") + 1))
Section E: Spot the Error
Q1
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).
Q2
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".
Q3
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.
Q4
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).
Q5
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.