Chr Python | What is chr() in Python

Chr Python | What is chr() in Python

In this tutorial, you will learn about the Python chr() method with the help of examples. We’ll discuss also the differences between chr() and ord() functions in Python.

What is chr() in Python?

The chr() in Python accepts a number as a parameter and then returns a character corresponding to its integer argument. The range of arguments can be provided to its numeric parameter (from 0 ++to 1,114,111).

When given an integer data type in Python, we often prefer to figure out what character it represents (basically known as the ASCII values). In Python, we have a function called chr() that accepts a number as a parameter and then returns a character. The range of arguments can be provided to its numeric parameter (0 to the number 1,114,111).

Chr Python | What is chr() in Python

chr() Syntax

chr(number)

chr() Parameter

ParameterDescription
numberAn integer representing a valid Unicode code point.
Integer number in the range 0 to 1,114,111

chr() Return Value

The return value of chr() are the following:

  • Unicode character – Unicode character of the corresponding integer argument (in the range 0 to 1,114,111)
  • ValueError – If an integer is out-of-range.
  • TypeError – If non-integer.

Python chr() with Integer Numbers

The example below shows how to use chr() function with integer numbers.

print("The character value of 99 is:",chr(99))
print("The character value of 182 is:",chr(182))
print("The character value of 80 is:",chr(80))

Output:

The character value of 99 is: c
The character value of 182 is: ¶
The character value of 80 is: P

Using the chr() method, the program converts different integers to their corresponding Unicode characters.

  • c is the Unicode character of 99.
  •  is the Unicode character of 182.
  • P is the Unicode character of 80.

Also read: Python bin Method in Simple Words with Example

Python chr() with Out of Range Integer

Python’s chr() method range is only from 0 to 1,114,111. If we put more than the range it will prompt an error.

print("The character value of -3000 is:",chr(-3000))
print("The character value of 9999999 is:",chr(9999999))

Output:

ValueError: chr() arg not in range(0x110000)

The example above provided an out-of-range integer argument to the chr() method. And the result of this argument is ValueError.

Python chr() with Non-Integer Arguments

In the previous example, we put out-of-range integers, but how about non-integer arguments such as string, boolean, etc?

Below shows the several examples of non-integer arguments to chr() method.

print("The character value of 'Prince' is:",chr("Prince"))
print("The character value of {1,2,3} is:",chr({1,2,3}))
print("The character value of ['a','b','c'] is:",chr(['a','b','c']))
print("The character value of 1.50 is:",chr(1.50))

Output:

TypeError: 'str' object cannot be interpreted as an integer
TypeError: 'set' object cannot be interpreted as an integer
TypeError: 'list' object cannot be interpreted as an integer
TypeError: 'float' object cannot be interpreted as an integer

The above example shows that putting non-integer arguments to the chr() will result in a TypeError.


What does chr do in Python?

What chr() does in Python is to convert the given integer to its corresponding Unicode value.

The chr() function in Python is a built-in function that is used to get any character for its corresponding Unicode value. The Unicode value from an integer is passed down as an argument in the chr() function. And then depending on the integer, this function will return a string character based on the Unicode value.

What is ord() and chr() in Python?

The Python ord() and chr() are part of the built-in functions in Python. They are used to convert an integer to a character or vice versa. Both functions work exactly opposite of each other.

Python chr() function is used to convert an Integer into a Unicode character.

While the Python ord() function is used to convert a character whose unicode to an Integer.

Additional information about Python ord() from Python official documentation, given a string representing one Unicode character, return an integer representing the integer unicode code point of that character.

If you want to know more about the ord() function you can check out our article about Ord in Python with Program Example.

Summary

In summary, chr() is the only function that is used to turn an integer into a Unicode character. The opposite function of the chr() Python is the ord() function.

Moreover, the chr() function can only take a single input and it must be an integer ranging from 0 through 1,114,111 (0x0FFF in base 16). And chr() will return a single Unicode character based on the corresponding integer.

Finally, if you found the information in this python tutorial useful, please consider sharing it with others who may find it useful as well.

Leave a Comment