Python chr() Function

Python Built In Chr() Function

This is a detailed tutorial of the Python built-in chr() Function. Learn to convert an integer into its corresponding Unicode Character with examples.

Python chr() Function

The function 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.

Syntax

Given below is the syntax for the chr() Function.

chr() Parameters

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 1 To 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 ValueError.

Examples

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 chr()

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))


Output.

Example 2. 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
print(chr(1114112))
print(chr(-5))

Output.

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!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *