This article will discuss the ord() function in detail by using different instances.
Ord() Exception Handling:
In this instance, the first user enters a number or character. For this entered number or character, we utilize a variable ‘inputCharacter.’ Then we make a new variable, ‘ordValue.’ Here, we use the ord() function. This function is used to get the ASCII value or Unicode value of an entered number or character.
For the execution of codes of python, spyder5 is installed and configured on the working system. First, a new project is created after tapping the “new file” button from the menu bar. The name of our new file is “temp.py46”.
Now we have to run the code. For running this program, press the button F5 from the keyboard. The user entered the number ‘35’, but the ASCII value of an entered number is not shown. There is an error that occurs. Because when we try to enter a number with length 2 an error will be raised. But when the user enters a single-digit ‘3’. The user gets the ASCII value of the ‘3’ number, which is ‘51’.
When we enter a character or number having two digits, the exception happens. So the length of the argument passed must be 1. The ord() function receives only one parameter.
Passing Hexadecimal Data:
We may pass an argument with an integer represented by different standard bases, such as hexadecimal (having base 16) format, to the ord() function. We may utilize hexadecimal numbers by preceding the integer through 0x.
Here we want to get the ASCII value of integer ‘14’ so we pass an argument ‘\x14’ in hexadecimal format to the ord() function. The print statement returns the Unicode value ‘20’ of integer ‘14’.
Passing different strings:
In this case, first, we want to get the ASCII value of any desired integer. So we take ‘6’ as an argument of the ord() function. After this, we want to know about the ASCII value of a character. For this, we pass an argument ‘X’ to the ord() function. At last, we want to get the Unicode value of any special character, and here we pass ‘&’ as an argument of the ord() function. The 128 Unicode values correspond to ASCII values.
The print statement first prints the ASCII value of ‘6’, which is ‘54’. Then prints the ‘88’, which is the Unicode number of the specified character ‘X.’ And at the end, prints the ASCII code value of ‘&,’ which is ‘38’.
Getting same Unicode values:
In this example, we take character Z in different forms. First, we pass “Z” as an argument to the ord() function to get the Unicode value of this character. For this, we use the variable ‘value.’ Next, we take another variable, ‘value1’. Now we pass ‘Z’ as an argument of ord() function. Using the ord() function, we get the ASCII values of both characters.
After running the code, we obtain the same output. This means the ASCII code of “Z” and ‘Z’ both are identical. This code will give ‘90’, which is the Unicode value of the alphabet ‘Z.’
The alphabet enclosed in double quotation marks and the alphabet enclosed in single quotation marks always has the same ASCII value.
Conclusion:
In this tutorial, we talk about the ord() function. Using the ord() function, we get the Unicode value of a given character or number. If the length of the entered number or character is more than 1, an error will occur. By using this function, we also get the ASCII value of integers having hexadecimal format. We also see that the Unicode values of characters surrounded by the double or single quotation marks are always the same. The ord() function is a Python built-in function that takes one Unicode number character as a parameter and then gives back the corresponding Unicode value or ASCII value of that given integer. We have seen different examples using the or () function, which takes the ASCII value of the passed parameter. This function has one parameter, and we must pass the string with length 1 as a parameter. The ord() function converts the character into an integer.
from https://ift.tt/3t9YsJ4
0 Comments