Python ord() Function

Python ord() function returns an integer representing the Unicode code point of the character.

This function is commonly used in situations where you need to perform operations on the numeric representations of characters, such as in encryption algorithms or sorting algorithms.

Syntax

ord(c)

Parameter

c: It is a parameter is any character whose length is 1.

Visual Representation of Python ord() Function

Example 1: Basic Usage

# code point of a digit
print(ord('6')) # Prints the Unicode code point of the digit '6'

# code point of uppercase alphabet
print(ord('A'))

# code point of lowercase alphabet
print(ord('x'))

# code point of special characters
print(ord('@')) 
print(ord('#')) 
print(ord('~')) 

Output

54
65
120
64
35
126

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

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

print(ord('AB'))

Output

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

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.