What is ord Function in Python?
Ord is a function (short of ordinal) which returns an integer representing the character passed to it. It means every character has some integer value, these characters cannot be read by computer directly, it first converts every character into the ASCII code. Python provides us ord function to generate ASCII code for any character and it will be an integer. ord function only takes a single character. You can pass this single character either in single quotes or double quotes also. You can also pass string value inside ord function but the length of the string should be 1, otherwise, ord function will return an error.
ASCII is a coding standard that assigns an integer value to every character on the keyboard. Every character has its integer value. The result will be different for both uppercase and lowercase characters.
Examples of ord Function in Python
In this article, we will discuss the ord function in python with the help of a few examples.
x = 'a'
As you can see in the above code, we have passed ‘a’ in the ord function
x = 'A'
So from the above example, you can see that the program has returned an integer value, this integer value is the ASCII code for ‘A’ i.e capital A. An important point to note here is, when we talk about ASCII code then A != a. It means the Capital alphabet is not equal to the small alphabet. They both have their own ASCII codes.
x = "A"
In the above program, we have Capital A in double-quotes, so its a string of length and ord function will accept and return the same integer value.
x = "Ab"
As you can see, this time we have “Ab”, its a string but the length of the string is 2 and the ord function was expecting the string length of 1.
x = "1"
In the above example, we can see that I have used integer value i.e 1 in the single quotes, so when we use double quotes it is a string, so now 1 is a string of length and we get the desired result.
x = '1'
In the above example, we have used 1 in a single quote so its a char now. so ord function will execute it successfully and we get the desired result.
x = 1
In the above example, we have the same value as previous code and this time we have directly used 1 inside the ord function, but ord function will not execute it as it is expecting a char or string of length 1 and ord function will return an error.
x = ' '
In the above program, you might be wondering we have mentioned an empty character and still org functions works, but it’s not empty it is a single space. Single space is also a character in the ASCII chart and its value is 32. Space is the first character in the ASCII chart.
x = '#'
In the above example, we have used special character and ord function will also work for the special character as well.
x = '.'
In the above example, I have ‘.’ as an input to an ord function and we have got the expected result.
Ord function takes a single character as an input or a string of length 1 and returns the integer i.e ASCII equivalent code. The computer only understands these ASCII codes, ASCII codes are converted into binary which is used by the computer to process information.
This is a guide to ord Function in Python. Here we discuss the Examples of ord Function in Python along with the outputs. You may also have a look at the following articles to learn more –