Python ord() function

Updated on Nov 10, 2019


The ord() function (short of ordinal) returns an integer representing the character passed to it. For ASCII characters, the returned value is 7-bit ASCII code, and for Unicode characters, it refers to the Unicode code point.

Its syntax is as follows:

ord(c) -> code point
PARAMETER DESCRIPTION
c c is the string character string.

Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
>>> 
>>> ord("A") 
65
>>>
>>>
>>> ord("f")
102
>>>
>>> 
>>> ord("á") # accented a
225
>>>
>>>
>>> ord("卍") # swastika
21325
>>> 
>>> 
>>> ord("😀") # Grinning Face 
128512
>>>

Try it out:

print(ord("A"))

print(ord("f"))

print(ord("á")) # accented a

print(ord("卍")) # swastika
 
print(ord("😀")) # Grinning Face 

To convert an integer returned by ord() back to its character equivalent we use chr() function.


Other Tutorials (Sponsors)