There are many languages and hence an unlimited number of symbols in this world. All the symbols are represented in a computer using different types of encoding such as ASCII and Unicode. In this article, we will see how we can find the ASCII value of a given character in Python.
What is the ASCII value?
ASCII stands for “American Standard Code For Information Interchange”. It contains only 128 characters that are used to represent different symbols, decimal digits and English alphabets in a computer.
In the ASCII encoding, each character has been assigned a numeric value between 0 to 127. This numeric value associated to a character is called the ASCII value of the character.
For example,
- “A” has been assigned the ASCII value 65. All the uppercase letters have been assigned the ASCII values after 65 in their respective order. i.e. “B” has an ASCII value of 66, “C” has an ASCII value of 67, and so on.
- “a” has been assigned the ASCII value 97. All the lowercase letters have been assigned the ASCII values after 97 in their respective order. i.e. “b” has an ASCII value of 98, “c” has an ASCII value of 99, and so on.
How to print the ASCII value of a character in Python?
To print the ASCII value of a given character, we can use the ord() function in python. The ord() function takes the given character as input and returns the ASCII value of the character. You can observe this in the following example.
char1 = "A"
result1 = ord(char1)
print("ASCII value of the character {} is {}.".format(char1, result1))
char2 = "B"
result2 = ord(char2)
print("ASCII value of the character {} is {}.".format(char2, result2))
char3 = "C"
result3 = ord(char3)
print("ASCII value of the character {} is {}.".format(char3, result3))
char4 = "Z"
result4 = ord(char4)
print("ASCII value of the character {} is {}.".format(char4, result4))
char5 = "a"
result5 = ord(char5)
print("ASCII value of the character {} is {}.".format(char5, result5))
char6 = "b"
result6 = ord(char6)
print("ASCII value of the character {} is {}.".format(char6, result6))
char7 = "c"
result7 = ord(char7)
print("ASCII value of the character {} is {}.".format(char7, result7))
char8 = "z"
result8 = ord(char8)
print("ASCII value of the character {} is {}.".format(char8, result8))
Output:
ASCII value of the character A is 65.
ASCII value of the character B is 66.
ASCII value of the character C is 67.
ASCII value of the character Z is 90.
ASCII value of the character a is 97.
ASCII value of the character b is 98.
ASCII value of the character c is 99.
ASCII value of the character z is 122.
If we pass a value other than a character to the ord()
function, it will raise a TypeError exception. For example, if we pass a string containing multiple characters instead of a single character, the ord()
function will raise TypeError as follows.
char1 = "ABC"
result1 = ord(char1)
print("ASCII value of the character {} is {}.".format(char1, result1))
Output:
Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 2, in <module>
result1 = ord(char1)
TypeError: ord() expected a character, but string of length 3 found
Similarly, if we pass an integer instead of a character to the ord() function, it will raise the TypeError exception as follows.
num1 = 123
result1 = ord(num1)
print("ASCII value of the character {} is {}.".format(num1, result1))
Output:
Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 2, in <module>
result1 = ord(num1)
TypeError: ord() expected string of length 1, but int found
Conclusion
In this article, we have discussed the ASCII encoding. We also saw how we can find the ASCII value of a character in Python. To know more about different values and allowed characters in python, you can read these articles on python literals and data types in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.