How to Convert ASCII to Char in Python

In this post, we will discuss how to convert ASCII to char in Python. Based on this program we will also develop a Python program to print all alphabet from ASCII value.

ASCII stands for American Standard Code for Information Interchange. It was developed by the ANSI (American National Standards Institute) and it is used to interchange the information from a high-level language to low-level language. Machine or Computer understand only binary languages. So, the character data type represents integers. For example, the ASCII value of the letter ‘A’ is 65.

Python Program to Convert ASCII to Char

We are using the chr() function to convert the ASCII value to the character. Which is a built-in function in Python that accepts a specified Unicode (ASCII value) as an argument and returns the character.

The syntax of chr() is:

chr(num)

Where num is an integer value.

chr() Parameters:

chr() method takes a single parameter, an integer i. The valid range of the integer is from 0 through 1,114,111.

Return value from chr():

The chr() method returns a character whose Unicode point is num, an integer. If an integer is passed that is outside the range then the method returns a ValueError.

# Python program to convert ASCII to character

# take input
num = int(input("Enter ASCII value: "))

# printing character
print("Character =", chr(num))

Output for the different input values:-

Enter ASCII value: 64
Character = @

Enter ASCII value: 69
Character = E

Enter ASCII value: 95
Character = _

Enter ASCII value: 107
Character = k

How to Print Alphabet From ASCII in Python

In the previous program, we will convert ASCII to a character but in this program, we will discuss how to print all alphabet from ASCII value (upper case and lower case). We are using map(), chr() and range() function to print all alphabet. The map() is a built-in function that applies a function on all the items of an iterator given as input.

# Python program to print all alphabet from ascii value

# using map() and char() function
list_upper = list(map(chr, range(65, 91)))
list_lower = list(map(chr, range(97, 123)))

# print uppercase alphabets
print('Uppercase Alphabets: ', list_upper)
# print lowercase alphabets
print('Lowercase Alphabets: ', list_lower)

Output:-

Uppercase Alphabets: [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’]
Lowercase Alphabets: [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’]

Also See:- Python Remove Character from String

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.