![]() |
VOOZH | about |
Python ord() function returns the Unicode code of a given single character. It is a modern encoding standard that aims to represent every character in every language.
Unicode includes:
For example, unicode of 'A' = 65 and '€' = 8364. Let's look at a code example of ord() function:
97 8364
Explanation:
ord(ch)
Parameter:
Return type: it returns an integer representing the Unicode code point of the character.
In this example, We are showing the ord() value of an integer, character, and unique character with ord() function in Python.
50 103 38
Note: If the string length is more than one, a TypeError will be raised. The syntax can be ord("a") or ord('a'), both will give the same results. The example is given below.
This code shows that ord() value of "A" and 'A' gives the same result.
Output
65 65The ord() function only accepts a single character. If the string length is more than 1, a TypeError will be raised.
Output:
Traceback (most recent call last):
File "/home/f988dfe667cdc9a8e5658464c87ccd18.py", line 6, in
value1 = ord('AB')
TypeError: ord() expected a character, but string of length 2 found
chr() function does the exact opposite of ord() function, i.e. it converts a unicode integer into a character. Let's look at an example:
65 A
Related articles: