How To Get Character From ASCII Value In Python

In this tutorial, we will learn how to get a character from its corresponding ASCII (American Standard Code for Information Interchange) value using Python. ASCII is a character encoding standard that assigns a numeric value to each character in the English alphabet, digits from 0 to 9, and several special characters.

We will use Python’s built-in function chr() and some basic error-handling techniques for invalid inputs.

Step 1: Understanding the chr() function

Python provides a built-in function called chr() that takes an integer (ASCII code) as its argument and returns the corresponding character. The function returns a Unicode string of one character. The range of valid code points is from 0 to 1,114,111 (0x10FFFF).

Here’s an example of how to use the chr() function:

This will output:


Step 2: Get user input for ASCII value

Now that we know how to use the chr() function, let’s ask the user to input an ASCII value. We’ll use the input() function to get the user input and convert it to an integer using int() function.

Make sure to handle the possible error that may occur if the user enters a non-numeric value. We’ll use a try-except block for error handling.

Step 3: Check if input is within ASCII value range

As mentioned earlier, the range of valid ASCII codes goes from 0 to 1,114,111. Therefore, we should check if the input provided is within this range.

Step 4: Display the character

If the input is within the valid ASCII range, we can use the chr() function to get the corresponding character and display it to the user.

Full Code

Here’s the full code combining all the steps:


In this tutorial, we learned how to get a character from its ASCII value using Python’s built-in chr() function. We also covered handling user input errors and checking if the input is within the valid ASCII value range. This knowledge is essential for text processing, encoding, and decoding tasks in Python.