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:
Python
print(ord('a'))
print(ord('€'))
Explanation:
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.
Python
print(ord('2'))
print(ord('g'))
print(ord('&'))
Example 2: Exploring ord() with Digits and SymbolsNote: 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.
This code shows that ord() value of "A" and 'A' gives the same result.
Python
v1 = ord("A")
v2 = ord('A')
print (v1, v2)
Output
65 65Example 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.
Python
Output:
Example 4: Using Both ord() and chr()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
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:
Python
v= ord("A")
print (v)
print(chr(v))
Related articles:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4