Python Tutorial Python Advanced Python References Python Libraries

Python chr() Function



The Python chr() function returns a string representing a character with specified unicode.

Syntax

chr(number)

Parameters

number Required. Specify an integer representing a valid unicode code point.

Example:

In the example below, the chr() function returns character with given unicode.

print(chr(65))

print(chr(97))

print(chr(49))

print(chr(64))

The output of the above code will be:

A
a
1
@

Example:

The chr() method can be used with iterables containing unicodes of the characters as elements.

MyList = [80, 121, 116, 104, 111, 110]

for i in MyList:
  print(chr(i), end=" ")

The output of the above code will be:

P y t h o n

Example: Out of range unicode

The chr() method raises exception when the specified unicode is out of range.

print(chr(10000000))

The output of the above code will be:

Traceback (most recent call last):
  File "Main.py", line 1, in <module>
    print(chr(10000000))
ValueError: chr() arg not in range(0x110000)

❮ Python Built-in Functions

5