Check your understanding of the previous reading: converting between characters and their ASCII codes with chr and ord.

Question 1

Convert all of the following numbers into their ASCII character code (as if you passed them into the chr function).

Do not include quotes.

48:

Show worked solution

chr(48) is 0, the digit zero: the digits 0 to 9 have the codes 48 to 57.

40:

Show worked solution

chr(40) is (, the left parenthesis.

74:

Show worked solution

chr(74) is J, uppercase J: the uppercase letters have the codes 65 (A) to 90 (Z); lowercase j is 106.

109:

Show worked solution

chr(109) is m, lowercase m: the lowercase letters have the codes 97 (a) to 122 (z); uppercase M is 77.

126:

Show worked solution

chr(126) is ~, the tilde, the last printable character.

Question 2

The ord function consumes a string, and the chr function returns a string. In both functions, this string is the same length. How many characters are in the string?

Show worked solution

ord needs a string of exactly one character (ord("FF") raises a TypeError), and chr returns one character, so the string has 1 character.

Question 3

Convert all of the following ASCII character codes into their integer representation (as if you passed them into the ord function).

!:

Show worked solution

ord("!") is 33, the exclamation mark.

.:

Show worked solution

ord(".") is 46, the period.

9:

Show worked solution

ord("9") is 57, the digit 9: the digits 0 to 9 have the codes 48 to 57.

H:

Show worked solution

ord("H") is 72, uppercase H: the uppercase letters have the codes 65 (A) to 90 (Z).

z:

Show worked solution

ord("z") is 122, lowercase z, the last lowercase letter (a is 97).