The ord() function in Python is a built-in function used to convert a character into its corresponding Unicode (ASCII) integer value. In simple terms, it returns the numeric code representing a given character.
Every character in a computer system is stored internally as a numeric value. The ord() function helps convert that character into its Unicode code point.
ord(character)
print(ord('A'))
Output
65
Explanation
print(ord('a'))
Output
97
Explanation
print(ord('5'))
Output
53
Explanation
'5' has a Unicode value of 53.print(ord('$'))
Output
36
Explanation
print(ord('☃'))
Output
9731
Explanation
ord() and chr()| Function | Purpose |
|---|---|
ord() |
Converts a character to its Unicode number |
chr() |
Converts a Unicode number to its character |
Example:
print(ord('A'))
print(chr(65))
Output
65
A
text = "Python"
for ch in text:
print(ch, ord(ch))
Output
P 80
y 121
t 116
h 104
o 111
n 110
Explanation
ord() is a built-in Python function used to convert a character into its Unicode integer value.