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.
Python Programming Exercises, Gently Explained
25
You can view each step of this program as it runs under a debugger at https://invpy.com/asciitable- debug/.