Python 3 reference · Built-in Functions

Python chr() Function

Return the one-character string for a Unicode code point, such as chr(65) giving 'A'. The inverse of ord().

Python 3 Since 3.0

Syntax

chr(i)

Parameters

ParameterDescription
i An integer code point from 0 to 1,114,111 (0x10FFFF).

Return value

A string of length 1.

Examples

Code points to charactersTry it yourself
print(chr(65), chr(97), chr(8364))
print(ord("A"))

Output

A a €
65
Build the alphabet a to fTry it yourself
print("".join(chr(c) for c in range(ord("a"), ord("f") + 1)))

Output

abcdef
A Caesar cipherTry it yourself
def shift(text, n):
    return "".join(chr((ord(c) - 97 + n) % 26 + 97) if c.islower() else c for c in text)

secret = shift("hello world", 3)
print(secret, "->", shift(secret, -3))

Output

khoor zruog -> hello world

Good to know

Values outside 0–0x10FFFF raise ValueError. For bytes (0–255) use bytes([n]) instead.

Version history

VersionChange
Python 3.0Added