ord And chr Map Code Points

ord returns the code point behind a character and chr inverts it, so char-level text math works on Unicode strings that iterate one code point at a time.

PythonCli Tooling

Code

pythonsnippet
11 lines
print("ord('A'):", ord("A"))
print("chr(9731):", chr(9731))
print("roundtrip:", chr(ord("A")) == "A")

# one code point per character: strings iterate by code point
text = "ab☃"
print("chars:", [ch for ch in text])
print("code points:", [ord(ch) for ch in text])

# both the \u and \N{} escapes produce the same character
print("escapes:", "\u2603", "\N{SNOWMAN}", "\u2603" == "\N{SNOWMAN}")

Output

Captured from a sandboxed run of this snippet in the build pipeline.

ord('A'): 65
chr(9731): ☃
roundtrip: True
chars: ['a', 'b', '☃']
code points: [97, 98, 9731]
escapes: ☃ ☃ True

How it works

Demo state sits in top-level bindings: text.

Caveats

  • Self-contained — imports go no further than what the runtime provides.

Applying it to real inputs

Look for the ord(, chr(9731):, roundtrip: prefixes once the file starts running; each one ties a printed line to the step that produced it.

Interactive Tools for This Topic

FAQ

Is the dependency story complicated?
Not at all: zero dependencies, so there is nothing to version or audit. Its state lives in module-level bindings: `text`.