ord() function in Python (original) (raw)

Last Updated : 27 Apr, 2025

**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:

[GFGTABS]
Python

``

`print(ord('a')) print(ord('€'))

`

``
[/GFGTABS]

**Explanation:

Syntax

ord(ch)

**Parameter:

**Return type: it returns an integer representing the Unicode code point of the character.

Examples of ord() Function

Example 1: Basic Usage of ord()

In this example, We are showing the **ord() value of an integer, character, and unique character with ord() function in Python.

[GFGTABS]
Python

``

`print(ord('2'))
print(ord('g'))
print(ord('&'))

`

``
[/GFGTABS]

**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.

Example 2: Exploring ord() with Digits and Symbols

This code shows that **ord() value of “A” and ‘A’ gives the same result.

[GFGTABS]
Python

``

`v1 = ord("A")

v2 = ord('A')

print (v1, v2)

`

``
[/GFGTABS]

**Output

65 65

Example 3: String Length > 1 Causes Error

The **ord() function only accepts a single character. If the string length is **more than **1, a **TypeError will be raised.

[GFGTABS]
Python

``

`print(ord('AB'))

`

``
[/GFGTABS]

**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

Example 4: Using Both ord() and chr()

**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:

[GFGTABS]
Python

``

`v= ord("A")

print (v)

print(chr(v))

`

``
[/GFGTABS]

Related articles: