Updated June 17, 2023
What is an ord Function in Python?
Ord is a function (short of ordinal) that returns an integer representing the character passed to it. It means every character has some integer value; the computer cannot read these characters directly; it first converts every character into the ASCII code. Python provides us ord function to generate ASCII code for any character, which will be an integer. The ord function only takes a single character. You can also pass this character in single quotes or double quotes. You can also pass string value inside the ord function, but the string length should be 1; otherwise, the 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' print(ord(x))
As you can see in the above code, we have passed ‘a’ in the ord function.
x = 'A' print(ord(x))
So the above example shows 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" print(ord(x))
In the above program, we have Capital A in double quotes, so a string of length and ord function will accept and return the same integer value.
x = "Ab" print(ord(x))
As you can see, we have “Ab” as a string this time, but the string length is 2, and the ord function was expecting the string length of 1.
x = "1" print(ord(x))
In the above example, we can see that I have used an 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' print(ord(x))
In the above example, we have used 1 in a single quote, so it’s a char now. So ord function will execute it successfully, and we get the desired result.
x = 1 print(ord(x))
In the above example, we have the same value as the previous code, and this time, we have directly used 1 inside the ord function, but the 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 = ' ' print(ord(x))
In the above program, you might be wondering if we have mentioned an empty character, and still, org functions work, 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 = '#' print(ord(x))
In the above example, we have used a special character, and the ord function will also work for the special character.
x = '.' print(ord(x))
In the above example, I have ‘.’ as an input to an ord function, and we have the expected result.
The 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 the computer uses to process information.
We hope that this EDUCBA information on “Perl print hash” was beneficial to you. You can view EDUCBA’s recommended articles for more information.