Python chr(): Convert Code Points to Characters — Syntax, Unicode Range, ASCII, Control Characters, Bytes vs Strings, ord() Relation, Pitfalls, and Practical Examples > dev

Skip to content

Entire search within the site

👈Go back dev

Python Python chr(): Convert Code Points to Characters — Syntax, Unicode Rang…

page info

name Goposu datetime⏰ 25-10-11 01:45 hit👁️ 12 comment💬 0

text

Python chr(): Convert Code Points to Characters — Syntax, Unicode Range, ASCII, Control Characters, Bytes vs Strings, ord() Relation, Pitfalls, and Practical Examples

Python chr(): Return the character for a Unicode code point

The built-in chr() function converts an integer Unicode code point to its corresponding character. While commonly used with ASCII (0–127), Python supports the full Unicode range (U+0000 to U+10FFFF), enabling conversion for emojis, symbols, and scripts worldwide.

Syntax

chr(i)

Parameter: i is an integer code point in the range 0 <= i <= 0x10FFFF.

Returns: A one-character str corresponding to i. Raises ValueError if out of range and TypeError if i is not an integer.

Quick examples (ASCII and beyond)

print(chr(65))       # 'A'          (ASCII)
print(chr(97))       # 'a'          (ASCII)
print(chr(48))       # '0'          (ASCII digit)
print(chr(8364))     # '€'          (U+20AC Euro sign)
print(chr(0x1F600))  # 'emoji'         (emoji U+1F600)

Relation to ord()

ord() is the inverse of chr(): it returns the code point for a given character.

c = 'A'
code = ord(c)          # 65
print(chr(code))       # 'A' (round trip)

ASCII, control characters, and escapes

ASCII covers code points 0–127. Control characters (0–31, 127) are non-printable (e.g., newline U+000A, tab U+0009). Use Python escape sequences when embedding them in strings.

print(chr(10))       # '\n' (newline)
print(chr(9))        # '\t' (tab)
print(chr(13))       # '\r' (carriage return)
print('\u0041')      # 'A' via Unicode escape

Unicode range and surrogates

Valid code points span U+0000..U+10FFFF. Python accepts surrogate code points (U+D800–U+DFFF) but they are not standalone characters; avoid them unless handling low-level encodings.

# Valid BMP example
print(chr(0x03A9))    # 'Ω' (Greek capital omega)
# Surrogates are discouraged in text processing
try:
    s = chr(0xD800)   # High surrogate (not a real character)
    print(repr(s))
except ValueError as e:
    print(e)

Bytes vs strings

chr() yields a Unicode string, not a byte. To convert integer bytes to text, decode using an encoding (usually UTF-8) or map bytes via chr only for ASCII-compatible cases.

b = bytes([65, 66, 67])   # b'ABC'
print(b.decode('ascii'))     # 'ABC'

# Mapping byte values to characters (ASCII-safe)
print(''.join(chr(x) for x in [65, 66, 67]))  # 'ABC'

Formatting, zero-padding, and code point display

Display code points in hex with format() or f-strings for clarity in logs and debugging.

ch = 'emoji'
print(f'U+{ord(ch):04X}')   # 'U+1F600'
print(f'{ord("A"):08b}')    # '01000001' (binary of 'A')

Common use cases

  • Generating character sets: Build ASCII or Unicode tables for teaching and testing.
  • Lexers/parsers: Translate code points to readable tokens.
  • UI and logging: Display symbols, icons, or diagnostic data.
# Printable ASCII table (32–126)
for i in range(32, 127):
    print(i, chr(i))

Pitfalls and caveats

  • Out-of-range values: chr(0x110000) raises ValueError.
  • Non-integers: Passing floats or strings raises TypeError.
  • Encoding mismatches: GUI/terminal fonts may not render all Unicode characters even if chr() succeeds.
  • Surrogates: Avoid standalone surrogates; use proper Unicode code points or UTF-16 handling if required.
try:
    print(chr(3.14))
except TypeError as e:
    print(e)  # integer required

try:
    print(chr(0x110000))
except ValueError as e:
    print(e)  # out of range

FAQ

Is chr() limited to ASCII?
No. It supports the full Unicode range up to U+10FFFF.
How do I get a newline or tab with chr()?
Use chr(10) for newline and chr(9) for tab, or the escape sequences '\n' and '\t'.
How do I convert characters back to integers?
Use ord(char). ord(chr(i)) == i for valid code points.

Related keywords

Python chr, Unicode code point, ASCII, control characters, ord, bytes vs strings, encoding, emoji, surrogate pairs

👍good0 👎nogood 0

comment list 0

There are no registered comments.

이미지 목록

전체 19건 1 페이지
게시물 검색
Copyright © https://goposu.com All rights reserved.
View PC version