Convert Int to ASCII in Python

ASCII, an abbreviation of American Standard Code for Information Interchange, can be defined as a standard that can assign numbers, letters, and some other characters in the 8-bit code that contains a maximum of 256 available slots.

This tutorial will discuss different methods to convert int to ASCII in Python.

Use the chr() Function to Convert int to ASCII in Python

There are in-built string functions in Python for string manipulations. The chr() function is one of them.

The chr() function is available to use in Python 3 and above and is utilized to provide the ASCII value of a corresponding ASCII code number.

The following code uses the chr() function to convert int to ASCII in Python.

a = chr(101)
print(a)

Output:

e

In the above code, the chr() function is applied on the number 101, which provides the output e.

In Python 2, the unichr() function can also be used instead of the chr() function. the unichr() function can provide the Unicode string for the number.

The following code uses the unichr() function to convert int to ASCII in Python.

# Python 2
a = unichr(101)
print(a)

Output:

e

There is no contrast between normal strings and Unicode in the newer versions of Python (Python 3 and above).

To convert the ASCII value back to int, we can use the ord() function. The ord() has the general purpose of acquiring a string of unit length and providing the Unicode equivalence of the string passed as an argument.

The ord() function is essentially used to achieve the reverse purpose of the chr() and the unichr() function. It has a fundamental syntax and is one of the in-built functions provided by Python.

The following code uses the ord() function to implement the inverse process of the chr() function in Python.

print (ord('e'))

Output:

101
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python Integer

  • Max Int in Python
  • Split Integer Into Digits in Python
  • Related Article - Python ASCII

  • Convert a Character to an Integer and Vice Versa in Python
  • Convert String to Integer in Python