Python Chr

The chr() function in Python is a built-in function that returns a string representing a character whose Unicode code point is the given integer. It is essentially the inverse of the ord() function, which returns the Unicode code point of a given character.

The chr() function takes an integer argument within the range 0 to 1,114,111 (0x10FFFF) and returns a single-character string corresponding to that code point. This function is particularly useful when working with Unicode characters, as it allows you to convert numerical values back into their respective characters.


Here’s the syntax of the chr() function:


Where i is the integer representing the Unicode code point of the character you want to retrieve. The returned value is a string containing a single character.


Here are a few examples to illustrate the usage of the chr() function:

print(chr(65))  # Output: 'A'
print(chr(8364))  # Output: '€'
print(chr(9731))  # Output: '☃'

In the first example, chr(65) returns the character ‘A’, as the Unicode code point for the capital letter ‘A’ is 65.

In the second example, chr(8364) returns the character ‘€’, which is the Euro symbol.

In the third example, chr(9731) returns the character ‘☃’, which is a snowman symbol.

It’s important to note that using chr() with invalid code points or values outside the range of 0 to 1,114,111 will raise a ValueError. Additionally, chr() only accepts integers as arguments. If you pass a float or any other non-integer value, a TypeError will be raised.

In summary, the chr() function in Python allows you to convert a Unicode code point into its corresponding character. It is a useful tool for working with characters and Unicode strings in Python programming.