ASCII

ASCII (American Standard Code for Information Interchange) is a 7-bit character encoding standard that represents 128 characters, including English letters, digits, punctuation marks, and control characters.

It forms the foundation of modern text encoding and is a subset of the Unicode character encoding standard.

Character Set

ASCII defines exactly 128 characters (0-127):

  • Control characters (0-31): Non-printable characters like newline, tab, carriage return
  • Printable characters (32-126): Space, letters, digits, punctuation
  • DEL (127): Delete character

Common ASCII Values

  • Digits 0-9: 48-57
  • Uppercase letters A-Z: 65-90
  • Lowercase letters a-z: 97-122
  • Space: 32
  • Newline: 10 (LF)
  • Tab: 9

Python and ASCII

Working with ASCII values:

Python
>>> # Get ASCII value of a character
>>> ord("A")
65
>>> ord("a")
97
>>> ord("0")
48

>>> # Get character from ASCII value
>>> chr(65)
'A'
>>> chr(97)
'a'
>>> chr(48)
'0'

ASCII string methods:

Python
>>> # Check if string contains only ASCII
>>> text = "Hello, World!"
>>> text.isascii()
True

>>> "Hello, 世界".isascii()

>>> # Encode to ASCII bytes
>>> text.encode("ascii")
b'Hello, World!'

ASCII and string operations:

Python
>> # Case conversion using ASCII offset
>>> chr(ord("a") - 32)
'A'

>>> # Check if character is ASCII letter
>>> def is_ascii_letter(char):
...     return "A" <= char <= "Z" or "a" <= char <= "z"
...

>>> is_ascii_letter("a")
True
>>> is_ascii_letter("ñ")
False

ASCII and Unicode

ASCII is the first 128 code points of Unicode (U+0000 to U+007F). This means that:

  • Every ASCII character has the same numeric value in Unicode
  • UTF-8 encodes ASCII characters as single bytes
  • ASCII files are valid UTF-8 files
Python
>>> # ASCII and UTF-8 produce identical bytes for ASCII characters
>>> "Hello".encode("ascii")
b'Hello'
>>> "Hello".encode("utf-8")
b'Hello'

Common Use Cases

  • Legacy Systems: Many older systems only support ASCII
  • Network Protocols: HTTP headers, email headers often restricted to ASCII
  • Configuration Files: Many formats require ASCII-only content
  • URLs: Must be ASCII-encoded (non-ASCII characters use percent encoding)

Handling Non-ASCII Characters

Python
>>> # Encoding errors with non-ASCII characters
>>> try:
...     "café".encode("ascii")
... except UnicodeEncodeError:
...     print("Can't encode non-ASCII characters")
...
Can't encode non-ASCII characters

>>> # Error handling strategies
>>> "café".encode("ascii", errors="ignore")
b'caf'
>>> "café".encode("ascii", errors="replace")
b'caf?'
>>> "café".encode("ascii", errors="xmlcharrefreplace")
b'caf&#233;'

Best Practices

  • Don’t assume text is ASCII - always handle encoding errors
  • Use UTF-8 instead of ASCII for new projects
  • When ASCII is required, validate input or use error handlers
  • Be explicit about encoding when opening files
  • Extended ASCII: Various 8-bit encodings (like Latin-1) that add 128 more characters
  • ANSI: Often misused to mean Windows code pages (not actually ASCII)
  • Code Page: Character encoding tables used in older systems
  • Mojibake: Garbled text from incorrect encoding/decoding

Tutorial

Unicode & Character Encodings in Python: A Painless Guide

In this tutorial, you'll get a Python-centric introduction to character encodings and unicode. Handling character encodings and numbering systems can at times seem painful and complicated, but this guide is here to help with easy-to-follow Python examples.

advanced python

For additional information on related topics, take a look at the following resources:


By Dan Bader • Updated June 30, 2025 • Reviewed by Leodanis Pozo Ramos