Facebook Page Twitter Page LinkedIn Page
× Python Tutorial Python Advanced Python Resources Python Libraries


The Python ord() function is used to return 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 below example, the ord() function is used to return 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:

TypeError: ord() expected a character, but string of length 2 found

❮ Python Built-in Functions