This is a detailed tutorial of the Python built-in chr() Function. Learn to convert an integer into its corresponding Unicode Character with examples.
chr() converts a given integer number into its corresponding Unicode Character. Every Unicode Character has a corresponding integer number. You can find the complete list of these integers & Unicode characters here.
Note. This function is the opposite of the ord() function.
Given below is the syntax for the
unicodeCharacter = chr(integer)
It takes only one argument and that is the integer for which you want to find out the corresponding Unicode Character. This integer value should be in the valid range i.e. from
11,14,111. (One To One million one hundred fourteen thousand one hundred eleven)
chr() Return Value
It returns the Unicode Character as a string that corresponds to the integer number passed in the argument of this function. In case, the integer number is out of the specified range, it raises the
The first example illustrates the usage of the Python built-in
chr() function to convert some of the integers into its corresponding Unicode characters and the second example shows the case if the integer passed in the argument of this function is out of the range.
Example 1. Converting Integers into Unicode Characters using
Here we’re simply using the
chr() function to convert some of the populary used integers into their corresponding unicode symbols or characters.
print("Hindi Letter A:", chr(2309))
print("English Letter A:", chr(65))
print("English Letter a:", chr(97))
print("Punjabi Letter A:", chr(2565))
print("Telegu Letter A:", chr(3077))
print("Dollar Symbol:", chr(36))
print("Indian Rupee Symbol:", chr(8377))
Hindi Letter A: अ
English Letter A: A
English Letter a: a
Punjabi Letter A: ਅ
Telegu Letter A: అ
Dollar Symbol: $
Indian Rupee Symbol: ₹
chr() supplied with an Invalid Integer Number
As already mentioned, if this method is supplied with an invalid integer number that is out of the available range, it raises the ValueError and this is exactly what’s happening in the following example.
#Invalid Range of Integer in Argument
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: chr() arg not in range(0x110000)
I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!