Python chr() Function
The Python chr() function is used to return 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 below example, the chr() function is used to return 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(150))
The output of the above code will be:
UnicodeEncodeError: 'ascii' codec can't encode character '\x80' in position 0: ordinal not in range(128)
❮ Python Built-in Functions