Python ord() Function with Examples

In the previous article, we have discussed Python Program for round() Function
ord() Function in Python:

The ord() function returns a number that represents the Unicode code of a given character.

Syntax:

ord(character)

Parameters

character: It is a Unicode character.

Return Value:

The ord() function returns the Unicode character as an integer.

Note: When the length of the string is not equal to one, a TypeError is thrown.

Examples:

Example1:

Input:

Given character = '4'
Given character = '$'
Given character = 'd'

Output:

The integer representing the given Unicode character 4 =  52
The integer representing the given Unicode character $ = 36
The integer representing the given Unicode character d = 100

Example2:

Input:

Given character = '0'
Given character = '@'
Given character = 'y'

Output:

The integer representing the given Unicode character 0 =  48
The integer representing the given Unicode character @ = 64
The integer representing the given Unicode character y = 121

Program for ord() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the character as static input and store it in a variable.
  • Apply ord() function to the given character which returns a number that represents the Unicode code of a given character.
  • Store it in another variable.
  • Print the integer representing the above given Unicode character.
  • The Exit of Program.

Below is the implementation:

# Give the character as static input and store it in a variable.
gvn_charctr = '4'
# Apply ord() function to the given character which returns a number that
# represents the Unicode code of a given character.
# Store it in another variable.
charctrs_unicode = ord(gvn_charctr)
# Print the integer representing the above given Unicode character.
print("The integer representing the given Unicode character",
      gvn_charctr, "= ", charctrs_unicode)

Output:

The integer representing the given Unicode character 4 =  52

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the character as user input using the input() function and store it in a variable.
  • Apply ord() function to the given character which returns a number that represents the Unicode code of a given character.
  • Store it in another variable.
  • Print the integer representing the above given Unicode character.
  • The Exit of Program.

Below is the implementation:

# Give the character as user input using the input() function and store it in a variable.
gvn_charctr = input("Enter some random character = ")
# Apply ord() function to the given character which returns a number that
# represents the Unicode code of a given character.
# Store it in another variable.
charctrs_unicode = ord(gvn_charctr)
# Print the integer representing the above given Unicode character.
print("The integer representing the given Unicode character",
      gvn_charctr, "= ", charctrs_unicode)

Output:

Enter some random character = B
The integer representing the given Unicode character B = 66

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.