Python

Python chr() Function

Python supports several inbuilt functions and modules that are used to make code easier and more efficient. To work with characters and strings in Python, different functions are used in Python. The “chr()” function is used to convert/transform the valid particular integer’s value into a Unicode character.

This write-up offers an in-depth overview of Python’s “chr()” function utilizing numerous examples via the following content:

What is the “chr()” Function in Python?

In Python, the “chr()” function is used to determine the character that represents the specified Unicode.

Syntax

Here is the general syntax of the particular function:

chr(number)

 

Parameters

In the above syntax, the “number” indicates the integer value having a valid Unicode point. This integer number is in the range “0” to “1,114,111”.

Return Value

The “chr()” function retrieves the Unicode character of the specified integer’s value.

Example 1: Determining the Unicode Character of the Specified Unicode Integers

In this example, the “chr()” function takes the specified valid Unicode integers, such as 97, 123, 65, and many more, as an argument that retrieves the character of the Unicode:

print(chr(97))
print(chr(123))
print(chr(65))
print(chr(1200))

 

The above code displayed the following Unicode character of the passed Unicode integers value:

Example 2: Determining Multiple Unicode Characters of the Specified Unicode Integers List

In the below code, the “for loop” iterates over the list of valid Unicode point values and retrieves the Unicode character using the “chr()” function. Take the following code as an example:

value = [97,123,65,1200]
for i in value:
    print(chr(i))

 

The below output snippet retrieves the Unicode character:

Example 3: Determining the Unicode Character for an Out-of-Range Number

In the below-provided example code, the “chr()” function takes the out-of-range number (01,114,111) as an argument and retrieves the “ValueError” to the output.

print(chr(-5))
print(chr(1114222))

 

The below error signifies the range that is passed to the “chr()” function is out of range:

Example 4: Determining the Unicode Character for Non-Integer Value

The below code is utilized to determine/calculate the Unicode character of the non-integer value. When the non-integer value is passed to the “chr()” function, the “TypeError” is retrieved to the console output:

print(chr('Joseph'))
print(chr('a'))

 

Above stated code generates the following output:

Bonus Tip: Converting Unicode Character Back to Unicode

In Python, to retrieve the Unicode integers of the specified Unicode character, the “ord()” function is used. Take the following code as an example:

print(ord('a'))
print(ord('{'))
print(ord('A'))
print(ord('Ұ'))

 

The below output snippet retrieves the integers of the Unicode character:

Conclusion

The inbuilt “chr()” function in Python is used to determine the character of the specified valid Unicode integers point. This function retrieves the character for the integers that lie in the specified range (01,114,111). The “ValueError” or “TypeError” can also occur when the character is out of range or non-integer value. This tutorial delivered a complete overview of the “chr()” function using several examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.