Prev - #6 Ordinal Suffix | Table of Contents | Next - #8 Read Write File

Exercise #7: ASCII Table


ASCII stands for American Standard Code for Information Interchange. Computers can only store numbers, so each letter, numeral, punctuation mark, and every other character is assigned a number called a code point. ASCII was a popular standard mapping of text characters to numbers. For example, 'Hello' would be represented by 72, 101, 108, 108, 111. Specifically, computers only store the ones and zeros of binary numbers. These decimal numbers translate to 01001000, 01100101, 01101100, 01101100, 01101111 in binary. An ASCII table showed all the characters and their assigned ASCII number values.

However, ASCII is an old and somewhat limited standard: it doesn’t have numbers assigned for Cyrillic or Chinese characters, for example. And it is an American standard: it has a code point for the dollar sign (code point 36) but not the British pound sign.

ASCII is no longer enough now that the internet has made global communication commonplace. The newer Unicode character set provides code points for every character and is what Python uses for its string values. Unicode’s code points are backward compatible with ASCII, so we can still easily use Python to display an ASCII table.

In this exercise you’ll learn how to use the ord() and chr() functions to translate between integers and text characters.

Exercise Description

Write a printASCIITable() function that displays the ASCII number and its corresponding text character, from 32 to 126. (These are called the printable ASCII characters.)

Your solution is correct if calling printASCIITable() displays output that looks like the following:

32 

33 !

34 "

35 #

--more--

124 |

125 }

126 ~

Note that the character for ASCII value 32 is the space character, which is why it looks like nothing is next to 32 in the output.

Try to write a solution based on the information in this description. If you still have trouble solving this exercise, read the Solution Design and Special Cases and Gotchas sections for additional hints.

Prerequisite concepts: for loops, range() with two arguments, chr()

Solution Design

Python’s built-in chr() function accepts an integer argument of a code point and returns the string of that code point’s character. The ord() function does the opposite: accepting a string of a single character and returning its integer code point. The “ord” is short for “ordinal”.

For example, enter the following into the interactive shell:

>>> ord('A')

65

>>> chr(65)

'A'

>>> ord('B')

66

>>> ord('!')

33

>>> 'Hello' + chr(33)

'Hello!'

The printASCIITable() function needs a for loop that starts at the integer 32 and goes up to 126. Then, inside the loop, print the integer loop variable and what the chr() function returns for that integer loop variable.

Special Cases and Gotchas

The upper bound argument to the range() function doesn’t include the number itself. This means that for i in range(32, 126): covers the range of integers from 32 up to, but not including, 126. If you want to include 126 (which we do for this exercise), pass 127 as the second argument to range().

Now try to write a solution based on the information in the previous sections. If you still have trouble solving this exercise, read the Solution Template section for additional hints.

Solution Template

Try to first write a solution from scratch. But if you have difficulty, you can use the following partial program as a starting place. Copy the following code from https://invpy.com/asciitable-template.py and paste it into your code editor. Replace the underscores with code to make a working program:

def printASCIITable():

    # Loop over integers 32 up to and including 126:

    for i in range(____, ____):

        # Print the integer and its ASCII text character:

        print(i, ____(i))

 

printASCIITable()

The complete solution for this exercise is given in Appendix A and https://invpy.com/asciitable.py. You can view each step of this program as it runs under a debugger at https://invpy.com/asciitable-debug/.

Further Reading

If you’d like to learn more about Unicode, I recommend Ned Batchelder’s “Pragmatic Unicode” blog post at https://nedbatchelder.com/text/unipain.html. He also gave a PyCon talk based on this blog post that you can watch at https://youtu.be/sgHbC6udIqc. Unicode is often seen as a complicated topic, but Ned breaks it down into understandable parts and what Python programmers need to know.

You can also see an ASCII table for yourself at https://en.wikipedia.org/wiki/ASCII.

Prev - #6 Ordinal Suffix | Table of Contents | Next - #8 Read Write File