How to Get the ASCII value of Char in Python

In this post, we will discuss how to get the ASCII value of char in python. Based on this program we will also develop a program to find the ASCII value of all characters in Python.

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.

It is case-sensitive. The same character, having a different format (upper case and lower case) has a different value. For example, The ASCII value of “A” is 65 while the ASCII value of “a” is 97.

Python Program to Find ASCII Value of Character

We are using the ord() function to convert a character to an integer (ASCII value). Which is a built-in function in Python that accepts a char (a string of length 1) as an argument and returns the Unicode code point for that character. We can use this function to find the ASCII value of any character. While ASCII only encodes 128 characters, the current Unicode has more than 100,000 characters from hundreds of scripts.

# Python program to find ASCII value of character

# take input
ch = input("Enter any character: ")

# printing ascii value of character
print("The ASCII value of " + ch + " is:", ord(ch))

Output for the different input values:-

Enter any character: a
The ASCII value of a is: 97

Enter any character: H
The ASCII value of H is: 72

Enter any character: q
The ASCII value of q is: 113

Program to Print ASCII Value of Characters in Python

In the previous program, we will discuss how to get the ASCII value of char in python, but in this program, we will discuss how to print the ASCII value of all characters (upper case and lower case).

# Python program to find ASCII value of all characters

#importing string function
import string

# printing ascii value of character
for c in string.ascii_letters:
    print(c,':', ord(c), end = ', ')

Output:-

a: 97, b: 98, c: 99, d: 100, e: 101, f: 102, g: 103, h: 104, i: 105, j: 106, k: 107, l: 108, m: 109, n: 110, o: 111, p: 112, q: 113, r: 114, s: 115, t: 116, u: 117, v: 118, w: 119, x: 120, y: 121, z: 122, A: 65, B: 66, C: 67, D: 68, E: 69, F: 70, G: 71, H: 72, I: 73, J: 74, K: 75, L: 76, M: 77, N: 78, O: 79, P: 80, Q: 81, R: 82, S: 83, T: 84, U: 85, V: 86, W: 87, X: 88, Y: 89, Z: 90

Also See:- Print Alphabets in Python

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

Leave a Comment

Your email address will not be published. Required fields are marked *