Open In App

ord() function in Python

Last Updated : 30 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python ord() function returns the Unicode code from a given character. This function accepts a string of unit length as an argument and returns the Unicode equivalence of the passed argument. In other words, given a string of length 1, the ord() function returns an integer representing the Unicode code point of the character when an argument is a Unicode object, or the value of the byte when the argument is an 8-bit string.

Example

Here, ord(‘a’) returns the integer 97, ord(‘€’) (Euro sign) returns 8364. This is the inverse of chr() for 8-bit strings and of unichr() for Unicode objects. If a Unicode argument is given and Python is built with UCS2 Unicode, then the character’s code point must be in the range [0..65535] inclusive. 

Python3




print(ord('a'))  # Output: 97
print(ord('€'))  # Output: 8364


Python ord() Syntax

Syntax:  ord(ch)

Parameter : ch – A unicode character

How does ord work in Python?

In this example, We are showing the ord() value of an integer, character, and unique character with ord() function in Python.

Python3




print(ord('2'))   
print(ord('g'))   
print(ord('&'))


Output :

50
103
38

Note: 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.

Demonstration of Python ord() function

This code shows that ord() value of  “A”, ‘A’ gives the same result.

Python




# inbuilt function return an
# integer representing the Unicode code
value = ord("A")
 
# writing in ' ' gives the same result
value1 = ord('A')
 
# prints the unicode value
print (value, value1)


Output

65 65

Error Condition while Using Ord(0)

A TypeError is raised when the length of the string is not equal to 1 as shown below.

Python3




# inbuilt function return an
# integer representing the Unicode code
# demonstrating exception
 
# Raises Exception
value1 = ord('AB')
 
# prints the unicode value
print(value1)


Output

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

Python ord() and chr() Functions

The chr() method returns a string representing a character whose Unicode code point is an integer.

Syntax: chr(num)

num : integer value

Where ord() methods work on the Unicodeopposite for chr() function.

Example of ord() and chr() functions

This code print() the Unicode of the character with the ord() and after getting the Unicode we are printing the character with the chr() function.

Python3




# inbuilt function return an
# integer representing the Unicode code
value = ord("A")
 
# prints the unicode value
print (value)
 
# print the character
print(chr(value))


Output

65
A


Previous Article
Next Article

Similar Reads

Python - Call function from another function
Prerequisite: Functions in Python In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with the help of multiple examples. What is Calling a Function
4 min read
Wand function() function in Python
function() function is similar to evaluate function. In function() function pixel channels can be manipulated by applies a multi-argument function to pixel channels. Following are the list of FUNCTION_TYPES in Wand: 'undefined''arcsin''arctan''polynomial''sinusoid' Syntax : wand.image.function(function, arguments, channel) Parameters : ParameterInp
1 min read
Returning a function from a function - Python
Functions in Python are first-class objects. First-class objects in a language are handled uniformly throughout. They may be stored in data structures, passed as arguments, or used in control structures. Properties of first-class functions: A function is an instance of the Object type.You can store the function in a variable.You can pass the functi
3 min read
Accessing Python Function Variable Outside the Function
In Python, variables defined within a function have local scope by default. But to Access function variables outside the function usually requires the use of the global keyword, but can we do it without using Global. In this article, we will see how to access a function variable outside the function without using "Global". Accessing Function Variab
3 min read
Python math.sqrt() function | Find Square Root in Python
sqrt() function returns square root of any number. It is an inbuilt function in Python programming language. In this article, we will learn more about the Python Program to Find the Square Root. sqrt() Function We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root
3 min read
wxPython - GetField() function function in wx.StatusBar
In this article we are going to learn about GetField() function associated to the wx.GetField() class of wxPython. GetField() function Returns the wx.StatusBarPane representing the n-th field. Only one parameter is required, that is, field number in status bar. Syntax: wx.StatusBar.GetField(self, n) Parameters: Parameter Input Type Description n in
1 min read
How to write an empty function in Python - pass statement?
In C/C++ and Java, we can write empty function as following // An empty function in C/C++/Java void fun() { } In Python, if we write something like following in Python, it would produce compiler error. # Incorrect empty function in Python def fun(): Output : IndentationError: expected an indented block In Python, to write empty functions, we use pa
1 min read
Ways to sort list of dictionaries by values in Python - Using lambda function
In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge to sort dictionaries according to their values can
2 min read
Python Numbers | choice() function
choice() is an inbuilt function in Python programming language that returns a random item from a list, tuple, or string. Syntax: random.choice(sequence) Parameters: sequence is a mandatory parameter that can be a list, tuple, or string. Returns: The choice() returns a random item. Note:We have to import random to use choice() method. Below is the P
1 min read
Python | askopenfile() function in Tkinter
While working with GUI one may need to open files and read data from it or may require to write data in that particular file. One can achieve this with the help of open() function (python built-in) but one may not be able to select any required file unless provides a path to that particular file in code. With the help of GUI, you may not require to
2 min read
Practice Tags :
three90RightbarBannerImg