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.

The Ord Function

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!

The Chr Function

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!

The ASCII Printable Characters

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.

CharNumberDescriptionCharNumberDescriptionCharNumberDescriptionCharNumberDescription
32space856digit 8P80uppercase Ph104lowercase h
!33exclamation mark957digit 9Q81uppercase Qi105lowercase i
"34quotation mark:58colonR82uppercase Rj106lowercase j
#35number sign;59semicolonS83uppercase Sk107lowercase k
$36dollar sign<60less-thanT84uppercase Tl108lowercase l
%37percent sign=61equals-toU85uppercase Um109lowercase m
&38ampersand>62greater-thanV86uppercase Vn110lowercase n
'39apostrophe?63question markW87uppercase Wo111lowercase o
(40left parenthesis@64at signX88uppercase Xp112lowercase p
)41right parenthesisA65uppercase AY89uppercase Yq113lowercase q
*42asteriskB66uppercase BZ90uppercase Zr114lowercase r
+43plus signC67uppercase C[91left square brackets115lowercase s
,44commaD68uppercase D\92backslasht116lowercase t
-45hyphenE69uppercase E]93right square bracketu117lowercase u
.46periodF70uppercase F^94caretv118lowercase v
/47slashG71uppercase G_95underscorew119lowercase w
048digit 0H72uppercase H`96grave accentx120lowercase x
149digit 1I73uppercase Ia97lowercase ay121lowercase y
250digit 2J74uppercase Jb98lowercase bz122lowercase z
351digit 3K75uppercase Kc99lowercase c{123left curly brace
452digit 4L76uppercase Ld100lowercase d|124vertical bar
553digit 5M77uppercase Me101lowercase e}125right curly brace
654digit 6N78uppercase Nf102lowercase f~126tilde
755digit 7O79uppercase Og103lowercase g