How to get the ASCII value of a character in Python?
ASCII [American Standard Code for Information Interchange] is a numeric value given to different characters and symbols which the computer stores and operates with.
For example, the ASCII value of A is 65, B is 66, etc.
To get the ASCII value of any character or any symbol, we use the ord() function. This function takes in a character as a parameter and returns its corresponding integer value[ here ASCII value].
character = 'h' print("The ASCII value of '" + character+ "' is", ord(character))
There are primarily two encodings Unicode and ASCII. Both are encoding techniques. The ASCII encodes about 128 characters and the Unicode encodes for about 100000 characters.
We can also find the ASCII values for a group of characters, i.e., a string. We’ve taken a user input for a string and stored it in string_eg. Now, we store the converted ASCII values of the characters of the string in ASCII_list.
string-eg = input("Provide example string: " ASCII_list=[ord(char) for char in string_eg] print(ASCII_list)
If we want to get a character from the given ASCII Value, we use the chr() function for Python 3 and unichr() for Python 2 versions.
character = 'h' print("The ASCII value of '" + character+ "' is", ord(character)) ASCII_Value= 104 print("The character is:", vchr(104))
Note: The ord() function doesn’t directly return the ASCII value, it returns the integer value in whatever encoding the character is in.