w3resource

Python: chr() function

chr() function

The chr() function returns the string representing a character whose Unicode codepoint is the integer.

Note that on narrow Unicode builds, the result is a string of length two for i greater than 65,535 (0xFFFF in hexadecimal).

Syntax:

chr(i)
Python: Built-in function - chr function()

Version:

(Python 3.2.5)

Parameter:

Name Description Required/
Optional
i A character whose Unicode codepoint is the integer i. he valid range for the argument is from 0 through 1,114,111 (0x10FFFF in base 16). ValueError will be raised if i is outside that range. Required

Return value:

Return the string representing a character whose Unicode codepoint is the integer i.

Example: Python chr() function

print(chr(65))
print(chr(97))
print(chr(124))

Output:

A
a
|

Example: Integer passed to chr() is out of the range

print(chr(-5))

Output:

Traceback (most recent call last):
  File "/tmp/sessions/0feab4034af997d8/main.py", line 1, in 
    print(chr(-5))
ValueError: chr() arg not in range(0x110000)

Python Code Editor:

Previous: callable()
Next: classmethod()

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.