The print() function allows you to easily make any character you can type on the keyboard appear on the screen. However, there are many other characters you may like to display: the hearts, diamonds, clubs, and spades card suits; lines; shaded boxes; arrows; music notes; and so on. You can obtain string values of these characters by passing their numeric code, called a Unicode code point, to the chr() function. Text is stored on computers as a series of numbers, with each character represented by a different number. This appendix contains a list of such code points.
Using the chr() and ord() Functions
Python’s built-in chr() function accepts an integer argument and returns a string of the number’s character. The ord() function does the opposite: it accepts a string argument of a single character and returns the character’s number. This number is the code point for the character in the Unicode standard.
For example, enter the following into the interactive shell:
Not all numbers are valid code points for printable characters. The terminal windows that show the text output of programs may be limited in which characters they can display. The font the terminal window uses also must support the character your program prints. The terminal window prints a Unicode replacement character, , for any character it is unable to print.
Windows’ terminal window has a far more limited range of characters it can display. This set is known as the Windows Glyph List 4, and it appears in this appendix and on Wikipedia at https://en.wikipedia.org/wiki/Windows_Glyph_List_4.
The code points for characters are often listed with a base-16 hexadecimal number, as opposed to the base-10 decimal numbers we are used to. Instead of the decimal digits 0 to 9, hexadecimal has the digits 0 to 9 and then continues with the letters A to F. Hexadecimal numbers are often written with a 0x prefix to denote that the number that follows is in hex.
You can convert a decimal integer value to a string of the hexadecimal number with the hex() function. You can convert a string of the hexadecimal number to a decimal integer with the int() function, passing 16 as the second argument. For example, enter the following into the interactive shell:
When calling the chr() function, you must pass a decimal integer as the argument, not a hexadecimal string.
Table of Code Points
The following are all the Unicode code points in the set known as Windows Glyph List 4, which are the characters supported by the Windows terminal program, Command Prompt. Both macOS and Linux can display more characters than are in this list, but to keep your Python programs compatible, I recommend you stick to the characters in this table.