Python Reference Library →

Python ord() Function – Get Unicode Code of a Character

Let’s look at ord() function – get unicode code of a character

  1. Notice

    ord() returns the Unicode code point of a single character. It does not accept a string containing several characters.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
character = 'A'
print(ord(character))
Live state

Top-level scope

character→Object 1
Object 1 · str'A'
This step

ord() returns the Unicode code point of a single character. It does not accept a string containing several characters.

Picture ord() function – get unicode code of a character

ord() returns the Unicode code point of a single character. It does not accept a string containing several characters.

Image brief

Alt text
A diagram for Python ord() Function – Get Unicode Code of a Character. Instructions: character = 'A' print(ord(character)). Displayed result: 65. The final example bindings are character --> object 1 (str). object 1: 'A'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python ord() Function – Get Unicode Code of a Character". Teaching focus: ord() returns the Unicode code point of a single character. It does not accept a string containing several characters. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS character = 'A' print(ord(character)) | v DISPLAYED RESULT 65 FINAL BINDINGS character --> object 1 (str) object 1: 'A' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python ord() Function – Get Unicode Code of a Character. Instructions: character = 'A' print(ord(character)). Displayed result: 65. The final example bindings are character --> object 1 (str). object 1: 'A'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
character = 'A'
print(ord(character))
Live state

Top-level scope

character→Object 1
Object 1 · str'A'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
65

What do you expect this program to print?

What does this example display?

character = 'A'
print(ord(character))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

65

Follow the printed values in order. The output is: 65

More examples and reference

Python ord() Function

The ord() function in Python returns the Unicode code point (an integer) for a given character. It is the reverse of the chr() function.

Syntax

ord(character)

Parameters:

  • character – A string representing a single Unicode character.

Returns:

  • An integer representing the Unicode code point of the given character.

Example 1: Using ord() with a lowercase letter

print(ord('a'))
97

Example 2: Using ord() with an uppercase letter

print(ord('A'))
65

Example 3: Using ord() with a special character

print(ord('@'))
64

Use Cases

  • Comparing characters based on Unicode values
  • Implementing custom sorting algorithms
  • Converting characters to numbers in cryptographic or hashing applications

ord() with Unicode Characters

print(ord('₹'))  # Rupee symbol
8377

This works with any character in the Unicode standard, not just English letters.

Common Mistakes

  • Passing more than one character: ord('ab') will raise a TypeError.
  • Passing an empty string: ord('') will also raise a TypeError.

Interview Tip

The ord() function is often used in problems that involve character frequency, shifting characters (like in Caesar cipher), or sorting letters.

Summary

  • ord() converts a character to its Unicode code point.
  • It accepts exactly one character.
  • Works with any character in the Unicode set.

Practice Problem

Write a program that takes a character as input and prints its Unicode value.

ch = input("Enter a character: ")
if len(ch) == 1:
    print("Unicode code point is:", ord(ch))
else:
    print("Please enter exactly one character.")

What to remember

Connect what you noticed in the example with the idea below.

Remember

ord() returns the Unicode code point of a single character. It does not accept a string containing several characters.

Watch out for this idea

ord() accepts any length of text.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →