MAKE SURE YOU COMPLETE THE QUIZ FOR THIS READING (THE NEXT ACTIVITY) BEFORE YOU CONTINUE!
The American Standard Code for Information Interchange (ASCII) is a way of representing text via numbers. Every single character is represented via a different number. Although modern systems require a larger set, the basic ASCII table has remained relatively unchanged since it was created in the 1960s. The system is critical to sending storing and transmitting textual data. You can read more about the history of ASCII here.
To convert a single character (string) to an integer, use the built-in ord function. This function consumes a string
and produces an integer. The string must be exactly one character long, or you will get a TypeError.
# Remember, you do not need to `print` to call a function like `ord`!
# We only need it here to see the results.
print(ord("A"))
# 65
print(ord("a"))
# 97
print(ord("6"))
# 54
print(ord("!"))
# 33
print(ord("FF"))
# TypeError!
To convert an integer back to a character (string), use the built-in chr function. This function consumes an
integer and produces a string. The integer must be a positive number, or you will get a ValueError. Values outside of
the range 32-126 are not printable, and may look weird when you try to convert them.
# Remember, you do not need to `print` to call a function like `ord`! # We only need it here to see the results. print(chr(65)) # "A" print(chr(97)) # "a" print(chr(54)) # "6" print(chr(33)) # "!" print(chr(-1)) # ValueError!
Codes 32-126 are common for all the different variations of the ASCII table. They are called printable characters, representing letters, digits, punctuation marks, and a few miscellaneous symbols. You will find almost every character on your keyboard.
Below, you can see all of the printable ASCII characters and their decimal representations.
| Char | Number | Description | Char | Number | Description | Char | Number | Description | Char | Number | Description | |||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 32 | space | 8 | 56 | digit 8 | P | 80 | uppercase P | h | 104 | lowercase h | ||||
| ! | 33 | exclamation mark | 9 | 57 | digit 9 | Q | 81 | uppercase Q | i | 105 | lowercase i | |||
| " | 34 | quotation mark | : | 58 | colon | R | 82 | uppercase R | j | 106 | lowercase j | |||
| # | 35 | number sign | ; | 59 | semicolon | S | 83 | uppercase S | k | 107 | lowercase k | |||
| $ | 36 | dollar sign | < | 60 | less-than | T | 84 | uppercase T | l | 108 | lowercase l | |||
| % | 37 | percent sign | = | 61 | equals-to | U | 85 | uppercase U | m | 109 | lowercase m | |||
| & | 38 | ampersand | > | 62 | greater-than | V | 86 | uppercase V | n | 110 | lowercase n | |||
| ' | 39 | apostrophe | ? | 63 | question mark | W | 87 | uppercase W | o | 111 | lowercase o | |||
| ( | 40 | left parenthesis | @ | 64 | at sign | X | 88 | uppercase X | p | 112 | lowercase p | |||
| ) | 41 | right parenthesis | A | 65 | uppercase A | Y | 89 | uppercase Y | q | 113 | lowercase q | |||
| * | 42 | asterisk | B | 66 | uppercase B | Z | 90 | uppercase Z | r | 114 | lowercase r | |||
| + | 43 | plus sign | C | 67 | uppercase C | [ | 91 | left square bracket | s | 115 | lowercase s | |||
| , | 44 | comma | D | 68 | uppercase D | \ | 92 | backslash | t | 116 | lowercase t | |||
| - | 45 | hyphen | E | 69 | uppercase E | ] | 93 | right square bracket | u | 117 | lowercase u | |||
| . | 46 | period | F | 70 | uppercase F | ^ | 94 | caret | v | 118 | lowercase v | |||
| / | 47 | slash | G | 71 | uppercase G | _ | 95 | underscore | w | 119 | lowercase w | |||
| 0 | 48 | digit 0 | H | 72 | uppercase H | ` | 96 | grave accent | x | 120 | lowercase x | |||
| 1 | 49 | digit 1 | I | 73 | uppercase I | a | 97 | lowercase a | y | 121 | lowercase y | |||
| 2 | 50 | digit 2 | J | 74 | uppercase J | b | 98 | lowercase b | z | 122 | lowercase z | |||
| 3 | 51 | digit 3 | K | 75 | uppercase K | c | 99 | lowercase c | { | 123 | left curly brace | |||
| 4 | 52 | digit 4 | L | 76 | uppercase L | d | 100 | lowercase d | | | 124 | vertical bar | |||
| 5 | 53 | digit 5 | M | 77 | uppercase M | e | 101 | lowercase e | } | 125 | right curly brace | |||
| 6 | 54 | digit 6 | N | 78 | uppercase N | f | 102 | lowercase f | ~ | 126 | tilde | |||
| 7 | 55 | digit 7 | O | 79 | uppercase O | g | 103 | lowercase g |