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
| Parameter | Description |
|---|---|
i |
An integer code point from 0 to 1,114,111 (0x10FFFF). |
Return value
A string of length 1.
Examples
print(chr(65), chr(97), chr(8364))
print(ord("A"))
Output
A a € 65
print("".join(chr(c) for c in range(ord("a"), ord("f") + 1)))
Output
abcdef
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
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
ord()· Return the Unicode code point (an integer) of a single character, such as ord('A') giving 65. The inverse of chr().ascii()· Return a printable representation of an object, like repr(), but with every non-ASCII character escaped.str()· Convert a value to its readable text form, or decode bytes into a string. Strings are immutable sequences of characters.- Python Strings · tutorial