Python ord() Function
The Python ord() function returns the number representing the unicode code of a specified character.
Syntax
ord(character)
Parameters
character |
Required. Specify a character whose unicode need to be found out. |
Example:
In the example below, the ord() function returns the number representing the unicode code of a specified character.
print(ord('A')) print(ord('a')) print(ord('1')) print(ord('@'))
The output of the above code will be:
65 97 49 64
Example:
The ord() method can be used with iterables containing characters as elements.
MyList = ['P', 'y', 't', 'h', 'o', 'n'] for i in MyList: print(ord(i))
The output of the above code will be:
80 121 116 104 111 110
Example: When String length is more than one
The ord() method raises exception when the string length is more than one.
print(ord('Hi'))
The output of the above code will be:
Traceback (most recent call last): File "Main.py", line 1, in <module> print(ord('Hi')) TypeError: ord() expected a character, but string of length 2 found
❮ Python Built-in Functions