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.
Syntax:
ord(character)
Examples of ord Function in Python
In this article, we will discuss the ord function in Python with the help of a few examples.
Example #1
Code:
x = 'a'
print(ord(x))
Output:
As you can see in the above code, we have passed ‘a’ in the ord function.
Example #2
Code:
x = 'A'
print(ord(x))
Output:
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.
Example #3
Code:
x = "A"
print(ord(x))
Output:
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.
Example #4
Code:
x = "Ab"
print(ord(x))
Output:
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.
Example #5
Code:
x = "1"
print(ord(x))
Output:
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.
Example #6
Code:
x = '1'
print(ord(x))
Output:
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.
Example #7
Code:
x = 1
print(ord(x))
Output:
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.
Example #8
Code:
x = ' '
print(ord(x))
Output:
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.
Example #9
Code:
x = '#'
print(ord(x))
Output:
In the above example, we have used a special character, and the ord function will also work for the special character.
Example #10
Code:
x = '.'
print(ord(x))
Output:
In the above example, I have ‘.’ as an input to an ord function, and we have the expected result.
Conclusion
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.
Recommended Articles
We hope that this EDUCBA information on “Perl print hash” was beneficial to you. You can view EDUCBA’s recommended articles for more information.