Built-in function

chr()

Turns a Unicode code point, a whole number such as 65, into the character it stands for, such as A. It is the opposite of ord().

BeginnerEvery example below runs in your browser. Change the code and press Run.

chr(i)

chr() takes a number and returns the one-character string that Unicode gives that number. chr(65) is 'A', chr(8364) is '€'.

It is the partner of ord(): ord() goes from character to number and chr() goes from number back to character. Together they let you do arithmetic on letters, for example to build the alphabet or shift letters in a simple code.

The number must be between 0 and 1,114,111 (0x10FFFF). Some numbers in that range are control characters that print as nothing visible, such as chr(10), which is a new line.

Parameters

ParameterWhat it is
i
required
A whole number from 0 to 1,114,111: the Unicode code point.

Return value

A string of length 1.

Examples

1Numbers to characters

Pythoneditable
print(chr(65))
print(chr(97))
print(chr(36))
print(chr(8364) == "€")
Output
A
a
$
True

chr(8364) is the euro sign: the comparison shows it gives exactly the character "€".

2Build the alphabet

Pythoneditable
letters = [chr(n) for n in range(ord("a"), ord("z") + 1)]
print("".join(letters))
print(len(letters), "letters")
Output
abcdefghijklmnopqrstuvwxyz
26 letters

3Column labels like a spreadsheet

Pythoneditable
headers = ["Name", "City", "Age", "Email"]
for i, title in enumerate(headers):
    print(chr(ord("A") + i), title)
Output
A Name
B City
C Age
D Email

4A Caesar cipher: shift every letter

Turn each letter into a number, add the shift, wrap around after z with % 26, and turn it back into a letter.

Pythoneditable
def shift(text, steps):
    result = ""
    for ch in text:
        if "a" <= ch <= "z":
            result += chr((ord(ch) - ord("a") + steps) % 26 + ord("a"))
        else:
            result += ch
    return result

secret = shift("meet at noon", 3)
print(secret)
print(shift(secret, -3))
Output
phhw dw qrrq
meet at noon

Common mistakes

Passing a string instead of a number

chr("65") raises TypeError. Convert text to a number with int() first.

Pythoneditable
code = "65"
try:
    chr(code)
except TypeError:
    print("TypeError: chr() needs a number, not text")
print(chr(int(code)))
Output
TypeError: chr() needs a number, not text
A

Going past the last code point

chr() raises ValueError for numbers below 0 or above 1,114,111. When you shift letters, wrap around with % as in the cipher example, so the result stays inside the range you want.

Questions people ask

How do I get the number of a character?

Use ord(): ord("A") gives 65.

How do I write a character from its hex code, like U+00E9?

Use chr(0xE9), or type it straight into a string with an escape: "\u00e9" is 'é'.

What is the difference between chr() and str()?

str(65) gives the text '65' (two digits). chr(65) gives 'A', the character whose code is 65.

Spotted a mistake on this page? Tell us