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 the ord function, but the length of the string 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 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.
Example #3
Code:
x = "A"
print(ord(x))
Output:
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.
Example #4
Code:
x = "Ab"
print(ord(x))
Output:
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.
Example #5
Code:
x = "1"
print(ord(x))
Output:
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.
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 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.
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 as well.
Example #10
Code:
x = '.'
print(ord(x))
Output:
In the above example, I have ‘.’ as an input to an ord function, and we have got 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 is used by the computer to process information.
Recommended Articles
This is a guide to ord Function in Python. Here we discuss the basic concept, examples of ord Function in Python, along with the outputs. You may also have a look at the following articles to learn more –
24 Online Courses | 14 Hands-on Projects | 110+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses