Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The ord() function in Python is used to return the Unicode code point of a given character. By passing a single character string to ord(), it provides the integer representation of that character's position in the Unicode table. This function is useful for converting characters to their numeric equivalents, which can be helpful in various text-processing tasks, such as encryption, decryption, or when working with character encoding. For example, ord('A') returns 65, the code point for the character 'A'.
Let us move further into the blog to clearly understand the topic: Python ord.
What is ord() in Python?
In the context of Python's ord function, the term ordinal refers to something that can be counted. It will return the integer value assigned to a character according to the Unicode encoding scheme. Python comes with a built-in function called ord().
In other words, it returns the numerical representation of a character's Unicode. This function can be helpful when converting between bytes to strings, binary to encoded representations, etc. A string with more than one character cannot have its Unicode returned.
Suppose we have given the input as 'o', then the Python ord will return the Unicode of the character, which is 111.
But what if we provide two characters instead of one? We will see its examples later.
Don't worry if you are not acquiring the Python ord function. We will see some examples in the later section of the blog for you.
Python ord Syntax
The syntax of Python ord is
ord(ch);
Here, ch represents any character of which we have to find the integer value.
Python ord Parameter
The Python ord function takes a single character as a parameter which is:
ch - a Unicode character
If we try to give two characters as parameters, it will throw some errors.
Python ord Return Value
The Python ord function returns an integer value of the Unicode character. Here, it's important to remember that the ord() function returns a Unicode value, not an ASCII value.
Now let us discuss some examples of the Python ord function to understand it better.
Python ord Examples
Let us discuss some examples of the Python ord function.
Code
Python
Python
order = ord('a') order_1 = ord('A')
print(order,order_1)
You can also try this code with Online Python Compiler
In the above example, ord('c') and ord('b') equal 99 and 98, respectively. Next, 99-98 = 1.
We hope everything regarding the Python ord function is clear to you. Let us proceed to the next section of the blog to discuss something more.
Python ord and chr functions
The ord function acts as an inverse to the chr function in Python. Suppose you are confused about how don't worry. Everything will be clear to you now.
Python's chr() returns a character-representation string and an integer with the Unicode code. For instance, chr(98) produces the character 'b' as its output. The parameter for this function is an integer.
Syntax
The syntax of Python ord is
ord(ch);
Here, ch represents any character of which we have to find the integer value.
The syntax of Python chr is
chr(i);
Here, i represents any integer of which we have to find the character value.
Example of ord() and chr() functions
Let us see examples of both functions and compare their outputs.
Code
Python
Python
# ord function order = ord('c') print(order)
# chr function order_new = chr(99) print(order_new)
You can also try this code with Online Python Compiler
In the output above, we can notice that the ord function returns 99 as an output. It was confirmed when we used the chr function to know the character representation of 99.
How does ord work in Python?
The ord() function in Python is used to get the Unicode code point of a given character. Unicode is a standardized character encoding that assigns each character a unique numeric value, known as a code point.
Here's a simple explanation of how ord() works:
char = 'A'
unicode_value = ord(char)
print(f"The Unicode code point of '{char}' is {unicode_value}")
In this example, ord() takes a single character (in this case, the uppercase letter 'A') as an argument and returns its Unicode code point. The result will be an integer representing the Unicode code point of the given character.
A Note on Unicode
Character encoding is a crucial and fundamental requirement for internationalization. The computer needs bytes for communication. There are more complex encodings as well as very simple ones.
One such character encoding scheme is Unicode. Unicode standardized the character encoding. Not all languages were sufficiently covered by the advent of ASCII characters. Unicode was therefore introduced to address this circumstance. The Unicode Consortium first presented this encoding method.
This standard has about 100000 characters, which represent characters from many languages. Unicode employs 4 bytes to represent characters instead of ASCII's 1 byte. As a result, it offers a huge selection of encoding. It comes in three varieties: UTF-8, UTF-16, and UTF-32. Among them, UTF-8 is the most frequently used.
Now let us examine some frequently asked questions of ord().
In the Unicode standard, the lowercase letter 'a' is assigned the code point 97, so ord('a') returns 97 in Python.
What is Ord (' Q ')?
ord('Q') returns the Unicode code point of the uppercase letter 'Q'. The specific value depends on the Unicode standard, but it's typically 81.
What is Ord (' B ')?
ord('B') returns 66, the Unicode code point for the character 'B'.
What does ord() 0 do in Python?
In Python, ord(0) raises a TypeError because ord() requires a string of a single character.
Conclusion
As we have reached the end of this blog, let us see what we have discussed so far. In this blog, we have discussed the Python ord function. Then we discussed its syntax, parameter, return value, and some examples. In the end, we discuss how the chr function differs from the ord function.
If you like to learn more, you can check out our articles: