The ord() function in Python accepts a string of length 1 as an argument and returns the unicode code point representation of the passed argument. For example
ord('B') returns 66 which is a unicode code point value of character ‘B’. In this tutorial, we will discuss the ord() function with the help of examples.
Note: The first 128 Unicode code point values are the same as ASCII
Python ord() function Example
In the following example we are finding the unicode code point values of an integer, a character and a special character. The first 128 unicode code points are same as ASCII values which means the unicode code points are same as the ASCII values of these passed strings of length 1.
# unicode code point of integer print("The ASCII value of 9 is",ord('9')) # unicode code point of alphabet print("The ASCII value of B is",ord('B')) # unicode code point of special character print("The ASCII value of ! is",ord('!'))
Another example of Python ord() function
The passed argument must have the length 1, if we try to pass a string with length more than 1 then we will get a compilation error. Lets take an example to see what error we get.
# unicode code point of integer print("The ASCII value of 10 is",ord('10'))
As you can see in the output that we got an error when we passed the string with length greater than 1.
Leave a Reply