Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Python ord
2.1.
Python ord Syntax
2.2.
Python ord Parameter
2.3.
Python ord Return Value
2.4.
Python ord Examples
2.4.1.
Code
2.5.
Python
2.5.1.
Output
2.5.2.
Error Condition Code
2.6.
Python
2.6.1.
Output
2.6.2.
Code
2.7.
Python
2.7.1.
Output
3.
Python ord and chr functions
3.1.
Code
4.
Python
4.1.
Output
5.
How does ord work in Python?
6.
A Note on Unicode
7.
Frequently Asked Questions
7.1.
What is the ORD () in Python?
7.2.
Why is ord a 97 Python?
7.3.
What is Ord (' Q ')?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Python ord() Function

Author Shivani Singh
0 upvote
gp-icon
Basics of Python
Free guided path
7 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Have you ever considered how to calculate the numeric value of any character? We will notify you how to do this in significantly less time. In this blog, we'll go through the fundamentals of the Python ord function, its parameters, return values, several examples, and many more.

Python ord

Python provides built-in utilities for converting between texts, values represented as numbers, and encoded data. The Python ord function returns the number corresponding to a particular character's Unicode. Let us move further into the blog to clearly understand the topic: Python ord. 

Python ord

In the context of Python's ord function, the term ordinal refers to something that can be counted. It will return the integer value assigned to a character according to the Unicode encoding scheme. Python comes with a built-in function called ord().

In other words, it returns the numerical representation of a character's Unicode. This function can be helpful when converting between bytes to strings, binary to encoded representations, etc. A string with more than one character cannot have its Unicode returned.

Suppose we have given the input as 'o', then the Python ord will return the Unicode of the character, which is 111.

But what if we provide two characters instead of one? We will see its examples later.

Don't worry if you are not acquiring the Python ord function. We will see some examples in the later section of the blog for you.

Python ord Syntax

The syntax of Python ord is

ord(ch);

 

Here, ch represents any character of which we have to find the integer value.

Python ord Parameter

The Python ord function takes a single character as a parameter which is: 

ch - a Unicode character

 

If we try to give two characters as parameters, it will throw some errors.

Python ord Return Value

The Python ord function returns an integer value of the Unicode character. Here, it's important to remember that the ord() function returns a Unicode value, not an ASCII value.

Now let us discuss some examples of the Python ord function to understand it better.

Python ord Examples

Let us discuss some examples of the Python ord function.

Code

  • Python

Python

order = ord('a')
order_1 = ord('A')

print(order,order_1)

Output

Python ord Example

Note that the ord values of a and A will be different.

 

Now let us notice what will happen if we try to give two characters in the parameter.

Error Condition Code

  • Python

Python

order = ord('aA')

print(order,order_1)

Output

Error Condition Code Example

We can see that a TypeError has been generated when we tried to pass two characters.
 

Now let us see one exciting example of ord().

Code

  • Python

Python

order = ord('c')
print(order)

order_1 = ord('b')
print(order_1)

order_new = ord('c') - ord('b')
print(order_new)

Output

Python ord Example

In the above example, ord('c') and ord('b') equal 99 and 98, respectively. Next, 99-98 = 1. 

We hope everything regarding the Python ord function is clear to you. Let us proceed to the next section of the blog to discuss something more.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Python ord and chr functions

The ord function acts as an inverse to the chr function in Python. Suppose you are confused about how don't worry. Everything will be clear to you now. 

Python's chr() returns a character-representation string and an integer with the Unicode code. For instance, chr(98) produces the character 'b' as its output. The parameter for this function is an integer.

Let us see examples of both functions and compare their outputs.

Code

  • Python

Python

# ord function
order = ord('c')
print(order)

# chr function
order_new = chr(99)
print(order_new)

Output

Python ord and chr functions Example

In the output above, we can notice that the ord function returns 99 as an output. It was confirmed when we used the chr function to know the character representation of 99.

How does ord work in Python?

The ord() function in Python is used to get the Unicode code point of a given character. Unicode is a standardized character encoding that assigns each character a unique numeric value, known as a code point.

Here's a simple explanation of how ord() works:

char = 'A'
unicode_value = ord(char)
print(f"The Unicode code point of '{char}' is {unicode_value}")

 

In this example, ord() takes a single character (in this case, the uppercase letter 'A') as an argument and returns its Unicode code point. The result will be an integer representing the Unicode code point of the given character.

A Note on Unicode

Character encoding is a crucial and fundamental requirement for internationalization. The computer needs bytes for communication. There are more complex encodings as well as very simple ones. 

One such character encoding scheme is Unicode. Unicode standardized the character encoding. Not all languages were sufficiently covered by the advent of ASCII characters. Unicode was therefore introduced to address this circumstance. The Unicode Consortium first presented this encoding method.

This standard has about 100000 characters, which represent characters from many languages. Unicode employs 4 bytes to represent characters instead of ASCII's 1 byte. As a result, it offers a huge selection of encoding. It comes in three varieties: UTF-8UTF-16, and UTF-32. Among them, UTF-8 is the most frequently used.

Now let us examine some frequently asked questions of ord().


Also see, Convert String to List Python

Frequently Asked Questions

What is the ORD () in Python?

ord() is a built-in Python function that returns the Unicode code point of a given character, representing the unique numeric value assigned to it in the Unicode standard.

Why is ord a 97 Python?

In the Unicode standard, the lowercase letter 'a' is assigned the code point 97, so ord('a') returns 97 in Python.

What is Ord (' Q ')?

ord('Q') returns the Unicode code point of the uppercase letter 'Q'. The specific value depends on the Unicode standard, but it's typically 81.

Conclusion

As we have reached the end of this blog, let us see what we have discussed so far. In this blog, we have discussed the Python ord function. Then we discussed its syntax, parameter, return value, and some examples. In the end, we discuss how the chr function differs from the ord function. 

If you like to learn more, you can check out our articles: 

You may refer to our Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, look at the interview bundle for interview preparations, and so much more!

Previous article
Python comparison operators
Next article
Math Ceil
Guided path
Free
gridgp-icon
Basics of Python
7 chapters
127+ Problems
gp-badge
Earn badges and level up
Live masterclass