What is Python chr() Function?

The Python chr() function is intended to assist you in converting Unicode code points into their respective characters. Whenever you have an integer that represents a Unicode code point, you can simply pass it as an input to Python chr() function. It will then do its magic and return the character associated with that particular code point. By utilizing this function, you’ll find it much easier to work with internationalization, text processing, and various encoding-related tasks within your Python programs.

Before delving into the numerous use cases of Python chr(), it is beneficial to first familiarize ourselves with its syntax, parameters, and the values it returns. This understanding will greatly aid in comprehending chr() in a comprehensive manner.

Python chr() Syntax and Parameter

When utilizing the chr() function in Python, you can leverage its simple and direct syntax. The syntax for using chr() is as follows:

result = chr(i)

When working with the Python chr() function, then remember that it takes only one parameter which is (i) .This parameter represents the Unicode code point, which is an integer value representing a specific character in the Unicode standard.

By gaining a deep understanding of this parameter and using them convinently, you have the ability to tailor the functionality of the chr() function to precisely meet your specific needs.

Python chr() return value

Python chr() will return a string that represents the character corresponding to the Unicode code point you provide. This enables you to work with and manipulate the character within your Python code. Let’s see an example to illustrate the usage and return value of the chr() function:

Example Code
# Converting Unicode code points to characters code_point = 65 character = chr(code_point) print(character)

In this example, we define a variable code_point with the value 65, which corresponds to the Unicode code point of the character ‘A‘. By using chr(code_point), we convert the code point to its corresponding character and store it in the character variable.


As observed in the output above, Python chr() function enables you to obtain the corresponding Unicode character for a given integer value conveniently.

What Does Python chr() do?

The main objective of Python chr() function is to transform Unicode code points into characters. This functionality allows you to easily convert and work with characters from various languages, symbols, emojis, and other Unicode entities. By utilizing chr(), you can conveniently handle character-related operations and manipulations within your Python code.

Now, let’s delve into the functionalities of the Python chr() function by showcasing its usage through intriguing examples.

I. Creating Object with chr()

When you create an object using chr() in Python, you can convert a Unicode code point into its respective character representation. The chr() function accepts an integer value, which represents a Unicode code point, and then returns the character associated with that particular code point. To provide a visual representation, let’s examine an example:

Example Code
code_point = 67 character = chr(code_point) print("Value of 67 is: ",character)

Here, we assign the integer value 67 to the variable code_point. Then, we use the chr() function with code_point as an argument to convert it into its corresponding character. The resulting character is assigned to the variable character. Finally, we print the value of the code_point using the print() function.

Value of 67 is: C

By using this example, you can easily obtain the Unicode representation of any desired integer value.

II. Python chr() Character Range

Python chr() supports a wide range of Unicode code points, allowing you to convert integers to characters within the Unicode character set. The Unicode standard encompasses a vast collection of characters from different scripts, languages, symbols, and emojis.

But it depends on the version of Python you are using. In Python 3.x, the range extends from 0 to 1,114,111 (0x10FFFF in hexadecimal notation), encompassing a vast array of characters. However, in the example below, only a subset of these characters will be covered. Let’s explore them:

Example Code
for code_point in range(65, 70): character = chr(code_point) print(character)

For this example, we take the range(65, 70) which generates a sequence of integers from 65 to 70, representing the Unicode code points for uppercase English letters (A to E). The chr() function is then used to convert each code point into its corresponding character. The resulting characters are printed one by one.


By using this above approach, you can leverage the chr() function to generate and work with characters from a specific range of Unicode code points.

III. Python chr() Character Out of Range

If you pass an integer that is outside the valid range of Unicode code points to the chr() function, Python will raise a ValueError. This happens when the integer is negative or exceeds the maximum value allowed for a code point. Let’s explore an example that shows the behavior of chr() with an out-of-range integer:

Example Code
code_point = -10 try: character = chr(code_point) print(character) except ValueError: print("Invalid code point!")

Here, we attempt to convert an out-of-range integer (-10) to a character using chr(). As the integer is not a valid Unicode code point, a ValueError is raised. We catch the exception using a try-except block and display a customized error message.

Invalid code point!

As observed in the output above, if you provide a negative integer or a value that is outside the range of characters supported by the chr() function, it will result in an error.

Now, let’s delve into the functionalities of chr() that enable seamless conversion of various Python data types.

Converting Different data types to chr()

The conversion of different data types to chr() in Python allows you to obtain the corresponding character representation of those data types. Here’s a summary of the conversions:

I. Integer to chr()

To convert integers to characters using Python chr() function, you simply pass the integer value representing the Unicode code point as an argument to chr(). The function then returns the character associated with that code point. Let’s take a look at an example:

Example Code
code_point_2 = 8364 character_2 = chr(code_point_2) print(character_2)

In this example, we have an integer variable code_point_2 assigned with the value 8364. We want to convert this integer value into its corresponding character representation using the chr() function. To achieve this, we use the chr() function and pass code_point_2 as an argument. This will convert the integer value 8364 into its corresponding character based on the Unicode code point.

Next, we assign the result to the variable character_2. Now, character_2 holds the character representation of the integer 8364.To observe the outcome of the conversion, we use the print() function to display the value of character_2


As you can see, the integer value 8364 is converted into the Euro currency symbol ‘‘ using the chr() function.

II. Python chr() to Integer

To convert a character to its integer code point, you can use the ord() function. Let’s say you have a character stored in a variable called character. By passing character as the input to the ord() function, it will return the Unicode code point value associated with that character. For example:

Example Code
character1 = 'P' character2 = 'Y' character3 = 'T' character4 = 'H' character5 = 'O' character6 = 'N' code_point1 = ord(character1) code_point2 = ord(character2) code_point3 = ord(character3) code_point4 = ord(character4) code_point5 = ord(character5) print(code_point1, code_point2, code_point3 , code_point4, code_point5)

Here, we have different variables named character1, character2, character3, character4, character5, and character6, each representing a different character. We want to convert these characters into their respective integer code points using the ord() function. Let’s go step by step:

First, we assign the character ‘P‘ to the variable character1, ‘Y‘ to character2, ‘T‘ to character3, ‘H‘ to character4’, and ‘O‘ to character5`. These are just arbitrary characters that we have chosen for this example. To convert these characters into their integer code points, we use the ord() function. We call ord(character1) to convert the character ‘P‘ into its corresponding integer code point, and assign the result to code_point1. We repeat this process for the remaining characters, assigning their respective code points to the corresponding variables.

Finally, we print the code points of the characters using the print() function. By passing code_point1, code_point2, code_point3, code_point4, and code_point5 as arguments to print(), we display their values in the output.

80 89 84 72 79

As you can see, each character is converted to its integer code point value using the ord() function. The code points are displayed in the output, separated by spaces.

III. Float to chr()

When using the chr() function in Python, it is important to note that it expects an integer Unicode code point as its argument. However, if you pass a floating-point number as an argument to chr(), you will encounter a TypeError.

Example Code
float_value = 97.5 try: character = chr(float_value) print(character) except TypeError: print("Invalid argument type!")

In this example, we attempt to convert a floating-point number (97.5) to a character using chr(). Since the function expects an integer, a TypeError is raised. We handle the exception using a try-except block and display a customized error message.

Invalid argument type!

As you can see in the above example, that you cannot convert float value into character.

IV. Python chr() to Float

The primary purpose of the chr() function in Python is to convert integer Unicode code points into their respective character representations. However, it is not designed to directly convert floating-point numbers into characters.

To obtain a character representation from a floating-point number, you must first convert it to an integer using suitable techniques like rounding, truncation, or utilizing floor/ceiling functions. Once the floating-point number is converted to an integer, you can utilize chr() to transform it into the corresponding character.

Consider the following example, which exemplifies the conversion of a floating-point number to a character representation:

Example Code
floating_number = 70.04 integer_number = int(floating_number) character = chr(integer_number) print(character)

Here, the floating-point number 70.04 is transformed into an integer using the int() function. Subsequently, the resulting integer is passed to chr() to retrieve the corresponding character.


By employing the aforementioned method, you can simply convert a character value into a float value.

Converting Non-Primitive Data Types to chr()

Converting non-primitive data types to chr() allows you to work with complex objects and data structures in your Python code. By applying the chr() function to non-primitive types, such as lists, tuples, sets, or custom objects, you can convert specific elements or values within these data types into their corresponding characters. This enables you to manipulate and process non-primitive data in a character-oriented manner, providing more flexibility and functionality in your code.

I. Converting a List to chr()

Converting a list to chr() involves iterating over the elements of the list and applying the chr() function to each element individually. Python chr() function converts an integer representing a Unicode code point into the corresponding character. Here’s an example to illustrate the conversion of a list to chr():

Example Code
integer_list = [65, 66, 67, 68, 69] character_list = [chr(num) for num in integer_list] print(character_list)

For this example, we utilize a list of integers, integer_list = [65, 66, 67, 68, 69], which represents Unicode code points. By iterating over each element in the integer_list and employing the chr() function, we collectively generate a list of characters. These resulting characters are stored in the character_list through a list comprehension. Lastly, we display the contents of character_list by printing it.

[‘A’, ‘B’, ‘C’, ‘D’, ‘E’]

By utilizing the capabilities of the chr() function, you have the ability to convert integer values in a list into their corresponding character representations.

II. Converting a Tuple to chr()

Converting a tuple to chr() code involves iterating over the elements of the tuple and applying the chr() function to each element individually. Since tuples are immutable, you need to create a new tuple with the converted characters. Here’s an example:

Example Code
my_tuple = (5, 16, 67) converted_tuple = tuple(chr(code_point) for code_point in my_tuple) print(converted_tuple)

In this example, the my_tuple contains integer values representing Unicode code points. By using a list comprehension and applying the chr() function to each code point, we create a new tuple converted_tuple with the corresponding characters

(‘\x05’, ‘\x10’, ‘C’)

As you can see, the elements of the tuple have been easily converted into their corresponding chr() values.

III. Converting a Set to chr()

Converting a set to chr() code involves iterating over the elements of the set and applying the chr() function to each element individually. Since sets are unordered collections of unique elements, the resulting chr() codes may not be in any particular order. Here’s an example:

Example Code
my_set = {72, 101, 108, 108, 111} converted_set = {chr(code_point) for code_point in my_set} print(converted_set)

Here, the my_set contains integer values representing Unicode code points. By using a set comprehension and applying the chr() function to each code point, we create a new set converted_set with the corresponding characters. Notice that duplicate values are automatically removed in the set,

{‘e’, ‘l’, ‘H’, ‘o’}

You can observe how simply the values of the set have been transformed into their respective chr() representations.

IV. Converting a Dictionary to chr()

Converting a dictionary to chr() code involves iterating over the dictionary’s values and applying the chr() function to each value individually. Since dictionaries in Python are unordered collections of key-value pairs, the resulting chr() codes may not be in any particular order. Here’s an example:

Example Code
my_dict = {'a': 61, 'b': 26, 'c': 17} converted_dict = {key: chr(code_point) for key, code_point in my_dict.items()} print(converted_dict)

For this example, the my_dict contains key-value pairs where the values represent Unicode code points. Using a dictionary comprehension, we iterate over the items of the dictionary and apply the chr() function to each value, creating a new dictionary converted_dict with the corresponding characters. The keys of the dictionary remain the same. The output shows the converted dictionary with the values replaced by their respective characters.

{‘a’: ‘=’, ‘b’: ‘\x1a’, ‘c’: ‘\x11’}

As you can see, the conversion of dictionary elements into their chr() values is done seamlessly and without any difficulty.

Python chr() Advance Examples

Moving forward, we will explore advanced illustrations of the Python chr() function to exemplify its versatility and extensive application possibilities. Through these examples, we will highlight how chr() excels in managing character-centric data, leveraging its capabilities and adaptable nature.

I. Printing Emojis Using chr()

With Python chr() function, you can even print emojis! Emojis are represented by specific Unicode code points, and chr() facilitates the conversion from these code points to the corresponding emoji characters. Consider the following example:

Example Code
code_point = 128512 emoji = chr(code_point) print(emoji)

For this example, we convert the Unicode code point 128512, which represents the “Grinning Face” emoji, to the corresponding emoji character using chr(). When we print emoji, we see the joyful grinning face emoji displayed.


As you can see, the code converts the code point 128512 into the corresponding emoji character, which represents a “Grinning Face“. It then prints the emoji character, displaying the joyful grinning face emoji.

II. Converting ASCII Values to chr()

When using Python chr(), you’re not limited to Unicode code points alone. You can also utilize it to convert ASCII values into characters. ASCII is a character encoding scheme that employs integers ranging from 0 to 127 to represent characters. Now, let’s explore an example to further understand how chr() can be used to convert ASCII values into characters:

Example Code
class ASCIIToCharacter: def __init__(self, ascii_value): self.ascii_value = ascii_value def convert_to_character(self): return chr(self.ascii_value) ascii_converter = ASCIIToCharacter(90) character = ascii_converter.convert_to_character() print("The value of 90 is:", character)

Here, we define a class ASCIIToCharacter with an __init__ method to initialize the ascii_value attribute. The class also has a convert_to_character method that utilizes the chr() function to convert the ASCII value to a character.

We then create an instance of the ASCIIToCharacter class, passing the ASCII value 90 as an argument. We call the convert_to_character method on the instance to perform the conversion and store the result in the character variable. Finally, we print the value of the ASCII value and the corresponding character.

The value of 90 is: Z

Through this example, you can easily understand how to use the ASCIIToCharacter class and the convert_to_character() method to convert an ASCII value to its corresponding character.

Now, let’s delve into some theoretical concepts related to the Python chr() function, which can greatly benefit your programming endeavors.

Considerations for using chr() with Non-ASCII Values

When you’re working with non-ASCII values and using the chr() function, there are a few important considerations to keep in mind:

I. Character Encoding

Ensure that the output encoding of your program supports the characters you are working with. Some encodings, such as ASCII, may not be able to represent all characters outside the ASCII range.

II. Output Representation

The way characters are displayed may vary depending on the environment or text editor you are using. Some characters, like emojis, may not render correctly in certain contexts.

III. Character Database

Characters and their interpretations can change over time as new versions of Unicode are released. Stay updated with the latest versions and character databases to ensure accurate handling and display of characters.

Common Use Cases  of  chr()

Let’s dive into some practical use cases and examples of how you can effectively utilize the chr() function in your programming.


chr() can be leveraged to create ASCII art by mapping ASCII values to corresponding characters. This allows you to generate various patterns, shapes, or even stylized text.

II. Character Frequency Analysis

By using chr() in combination with string manipulation and data analysis techniques, you can perform character frequency analysis to determine the occurrence and distribution of characters in a text.

III. Unicode Manipulation

With the support for Unicode characters, chr() empowers you to handle diverse writing systems, mathematical symbols, special characters, and emojis. This is particularly valuable when working with multilingual applications or processing user-generated content.

Congratulations on completing the Python chr() function! You have successfully grasped its concepts and potential. It’s time to harness the power of chr() in your Python programming endeavors. By using this function, you can unlock a world of possibilities in working with characters, handling internationalization, and manipulating text.

Remember, chr() goes beyond just converting Unicode code points; it can also transform ASCII values into characters. This flexbility allows you to expand your coding horizons and explore new creative avenues. So, use the capabilities of chr() and let it be your ally in expressing and manipulating characters, symbols, and emojis.

As you continue your programming journey, don’t shy away from utilizing chr() to its fullest potential. Dive into exciting applications like creating objects, converting different data types, and even printing emojis. Embrace the opportunities it brings and let your code communicate in the language of characters, opening doors to endless possibilities.

Keep coding, keep exploring, and let your imagination soar with the Python chr() function. Embrace its power and witness the transformative impact it can have on your projects. Enjoy the journey and unleash your creativity with chr() by your side!

Scroll to Top