Python uses the same code points as ASCII for every value from 0 through 127, so its built-in ord() and chr() functions give you a direct mapping between characters and the American Standard Code for Information Interchange. When ord('A') returns 65 and chr(65) returns 'A', those numbers are the same decimal, hexadecimal, octal, and binary values defined in the original 7-bit ASCII standard from RFC 20 and ECMA-6. Standard ASCII covers exactly 128 code positions starting at decimal 0 and ending at decimal 127; bytes 128 through 255 are not part of standard ASCII and belong to encoding-specific extensions like ISO-8859-1 or Windows-1252. Python 3 strings are stored as Unicode code points, which means ord() returns a value above 127 for accented letters, emoji, or CJK characters. For ASCII-specific work such as parsing text files, decoding network packets, or building binary protocols, you need an accurate reference for the standard values. The ASCII Table tool provides those values with cross-checked names and prefixes, so you can confirm what ord() is actually returning before you use it in code.

how to use ascii code in python
how to use ascii code in python

ASCII and Python: The Built-in Connection

Python does not have a separate "ASCII type" for ordinary strings, but the language was designed so that the first 128 code points of Unicode align with ASCII. This is not coincidence: Unicode explicitly reserved U+0000 through U+007F for ASCII compatibility, which is why every standard ASCII value behaves identically whether you treat it as ASCII or Unicode.

When you call ord(c) on a single ASCII character, you get the same number that a strict ASCII table would show. The character 'A' returns 65 because U+0041 in Unicode is the same code position as 65 in ASCII. The same holds for lowercase letters (a returns 97), digits ('0' returns 48), punctuation ('!' returns 33), and the non-printing controls like '\n' (10), '\t' (9), and '\0' (0). For any integer in the range 0 through 127, chr(i) returns the matching character.

This alignment matters in practice. You can parse plain ASCII files with ord() and chr() without any special mode, decode HTTP headers with the ASCII codec, and write byte sequences to network sockets knowing that the lower half of every byte follows the ASCII standard. To look up the exact values, the ASCII Table tool organizes the full 128 entries with verified names and prefixed radix notations drawn from RFC 20 and the Unicode Basic Latin chart.

ord() and chr(): Converting Between Characters and Integers

The two most direct Python tools for working with ASCII codes are the built-in ord() and chr() functions.

ord(c) takes a single-character string and returns its integer code point. For ASCII characters, that code point is the decimal ASCII value:

  • ord('A') returns 65
  • ord('a') returns 97
  • ord('0') returns 48
  • ord(' ') returns 32
  • ord('\n') returns 10

chr(i) does the reverse, returning the character for an integer code point:

  • chr(65) returns 'A'
  • chr(97) returns 'a'
  • chr(48) returns '0'
  • chr(32) returns ' '

For radix conversions in Python you can use the built-in hex(), oct(), and bin() helpers, which all return strings with the radix prefix already attached:

  • hex(65) returns '0x41'
  • oct(65) returns '0o101'
  • bin(65) returns '0b1000001'

These match exactly what the ASCII Table shows for the row 'A'. The fixed-width prefixes in both the tool and the Python output make it clear when a value is hexadecimal, octal, or binary so you never confuse, for example, 0o101 (decimal 65) with 101 (decimal one hundred and one).

Python's ASCII Methods at a Glance

The most common Python operations on ASCII data fit a small number of patterns. The table below summarizes them with the direction, an example expression, and the result.

Method Direction Example expression Result
ord(c) char → int ord('A') 65
chr(i) int → char chr(65) 'A'
str.encode('ascii') str → bytes 'A'.encode('ascii') b'A'
bytes.decode('ascii') bytes → str b'A'.decode('ascii') 'A'
bytes[i] bytes → int b'A'[0] 65
int.to_bytes() int → bytes (65).to_bytes(1, 'big') b'A'

These six patterns cover most everyday ASCII work in Python. The first two translate between characters and integers in your code; the middle two round-trip between text and bytes for I/O; the last two build or inspect raw byte sequences for binary protocols.

How to Look Up ASCII Codes with the ASCII Table

When you need an exact ASCII value for documentation, a code comment, or a binary protocol, opening a reliable reference beats guessing. The ASCII Table tool lists all 128 standard code positions with verified decimal, two-digit hexadecimal, three-digit octal, and seven-bit binary values.

Follow these steps to look up any ASCII value:

  1. Open the ASCII Table and scroll through the complete 128 rows, or use the search box. You can type a visible character, a name like "line feed," or a control abbreviation such as "LF" or "NUL."
  2. For an exact lookup by code, enter the value with the right prefix: plain digits for decimal (65 finds capital A), 0x for hexadecimal (0x41 also finds A), 0o for octal (0o101 again finds A), or 0b for binary (0b1000001 finds A). For an explicit literal character, use the char:value form, such as char:0 or char:space.
  3. Use the category filter to narrow the table to controls, punctuation, digits, uppercase letters, lowercase letters, Space, or Delete when you want to focus on one range.
  4. Review the decimal, hexadecimal, octal, and seven-bit binary values together so you can confirm the same code in every notation. The prefixes in each column make the radix explicit, which is useful when reading network traces or command-line output.
  5. Select Copy on the row you need to put the complete row, including the display value, name, and all four number systems, on your clipboard. The copy is done locally in your browser and depends on the standard browser clipboard permission; if permission is denied, the page reports the failure instead of claiming success.

This last step is helpful for code comments, bug reports, protocol notes, and teaching material where you want the reader to see all four notations at once without switching tools. The same approach works for broader programming and data tasks covered in the general ASCII programming guide.

Working with Bytes and ASCII Literals

Bytes objects in Python store sequences of small integers, and for the 0-127 range each integer is also an ASCII code. A literal like b'A' is a one-byte bytes object holding the integer 65, which is the same value ord('A') returns.

Three patterns come up repeatedly. First, encoding a string to bytes with the ASCII codec gives a predictable bytes object: 'Hello'.encode('ascii') produces b'Hello' because each character falls inside the ASCII range. Second, indexing a bytes object yields the integer code: b'A'[0] returns 65, and list(b'ABC') returns [65, 66, 67]. Third, building bytes from a list of integers works when those integers are in the ASCII range: bytes([65, 66, 67]) returns b'ABC'.

If you try to encode a character above 127 with the ASCII codec, Python raises a UnicodeEncodeError. This is intentional; the ASCII codec enforces the 7-bit boundary, and that boundary is what makes ASCII a universal interchange format. When you need to encode non-ASCII characters, you have to pick a different codec such as 'utf-8' or 'latin-1', and the choice changes which bytes you get.

Control Characters and Special Codes

ASCII reserves decimal 0 through 31 plus decimal 127 for control codes rather than printable glyphs. Python represents these in source code with escape sequences that match their ASCII values:

  • '\0' is NUL (decimal 0)
  • '\t' is HT, the Horizontal Tabulation (decimal 9)
  • '\n' is LF, the Line Feed (decimal 10)
  • '\r' is CR, the Carriage Return (decimal 13)
  • '\x1b' is ESC, the Escape character (decimal 27)
  • '\x7f' is DEL, the Delete character (decimal 127)

The names and abbreviations follow RFC 20, the original ASCII standard from 1969. When you print these characters in a terminal or write them to a file, they usually trigger an action such as advancing a line, moving a cursor, or terminating a string rather than showing a glyph.

Decimal 32 is the space character SP, which is a graphic character that happens to be invisible. Decimal 127 is DEL, and RFC 20 explicitly notes that DEL is not a control character in the strict sense; that is why many references, including the ASCII Table tool, place it in its own Delete category. Both SP and DEL have their own roles in protocols and text processing, and Python exposes them through the usual escape sequences ('\x20' and '\x7f') or the literals ' ' and '\x7f'.

Why ASCII Stops at 127

Standard ASCII is a 7-bit set, which gives exactly 128 code positions. The eighth bit of an older 8-bit byte has been used in many different ways: ISO-8859-1, Windows-1252, and CP437 each assign different characters to values 128 through 255. There is no single "extended ASCII" mapping that covers all of them, and calling any of these mappings ASCII is misleading because they are not part of the original standard.

In Python, a Unicode string stores code points above 127 without trouble. ord('é') returns 233 and ord('中') returns 20013, which are valid Unicode code points but not ASCII values. When you encode such a string with a non-ASCII codec, you get a specific byte sequence, but those bytes depend entirely on the encoding. This is why the ASCII Table tool limits itself to decimal 0 through 127; if you need to decode a byte above 127, you must first know which encoding produced it, and that decision is part of the decoding problem, not something the byte value alone can answer.

Practical Tasks Where ASCII Codes Matter in Python

A few common tasks rely on knowing exact ASCII values. Counting letters by position in the alphabet is faster with integers than with character comparisons. For a single lowercase letter c, the formula is position = ord(c) - ord('a') + 1. Substituting c = 'c' gives position = 99 - 97 + 1 = 3, so 'c' is the third letter. Detecting control characters in a log file means checking for values below 32 or exactly 127, often written as if 0 <= b < 32 or b == 127 in a loop over a bytes object. Building a fixed binary header for a network packet means writing specific bytes in a known order, where each byte maps to a documented ASCII code.

For any of these tasks, the safest practice is to verify the values with a standards-backed reference rather than relying on memory. The cross-checked values from RFC 20, the Unicode Basic Latin chart, and ECMA-6 used by the ASCII Table tool make that step quick. Search by decimal, hex, octal, binary, name, or abbreviation, copy the row you need, and confirm every code position before it leaves your editor.