×

chr() and ord() Functions in Python

Python language contains many built-in functions which we use for many programs to work efficiently. The chr() and ord() are a few built-in functions in Python that are used for conversions. These both functions have their separate conversion functions. In this article, let us discuss the chr() function and the ord() function. We will complete these functions by understanding some example codes and references.

Understanding the Odr() function:

The ord() is a built-in function in python language that is used to convert a specific character into an integer. The ord() function will take a string argument from a single code character and outputs its integer value. Ord() function is opposite to the char() function

The syntax we use for this function is as follows:

ord(a)

now let us look at an example code

Let us take a string of length one, which will return an integer presenting the Unicode. When an argument is a Unicode object, it will be the value of an 8-bit string. So we have to make sure the input we take for conversion must be of length "1." For this example, we take the code as

code 1:

print(ord('A'))
print(ord('a'))
print(ord('@'))
print(ord('b'))
print(ord('#'))
print(ord('L'))

Output:

65
97
64
98
35
76

Code 2:

print(ord('hello'))

Output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: ord() expected a character, but string of length 5 found
>

So as we can see in code 1, we used the Unicode object and the ASCII table to get the values.

chr() and ord() Functions in Python

For code 2, we gave input of 5 characters, and once we ran the code, we got an error message because the ord() method only accepts single characters, not multiple characters.

For a clear picture, let us look below at the ASCII table for reference values for every input character we take.

With this, we got an idea of what the ord() function and their works.

Understanding the chr() function:

This chr() is a python's built-in function that is used for converting an integer into a special character. There is a specific range to find the value of the integer, and the range lies between 0 to 1,114,111. We have to ensure that the integer input we give must be between this range, or we will get an error in the output. This function works all opposite to the ord() function.

The syntax we use for this function is given below

String chr(n)

n is where we give the integer input of range 0 to 1,114,111.

To understand this concept better, let us look at an example code.

Code:

print(chr(3))
print(chr(5))
print(chr(88))
print(chr(100))
print(chr(40))
print(chr(150))

Output:

 


X
d
(
?
>

Looking at the above code, we took some integers as the values for n and got the respected conversions as the output.

Code 2:

print(chr(-5))

Output:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: chr() arg not in range(0x110000)

In this code 2, we gave an integer that is not taken from the range of n, so we got an error as an output. It is very important to provide an integer between the range as an input.

Let us look at the Unicode character table used for giving outputs based on the input integers.

chr() and ord() Functions in Python

Using this table, we will understand on What bases we will get our outputs.

Hexa decimal data:

In the ord() and chr() functions, we can also pass integers in the hexadecimal format, like (base 15). We will get proper outputs only if we add a prefix to the integer. To understand this concept better, let us look at an example code

Code:

print(hex(18))
print(chr(0x16))
print(ord('\x12'))

Output:

0x12


18

This code passed the hexadecimal value in the chr() and ord() functions. And got the respective outputs.

With this, we got an idea of the chr() function and its working.

Conclusion:

So far, in this article, we have covered a few built-in functions of python, which are chr () and ord(). These functions are mainly used for conversions. We learned about the essential functions of these conversions and the working of these functions, such as the syntax of the functions and the example codes used for the chr to int and int to char conversions. Finally, we have seen about the hexadecimal data where we learned that in python, we could also use the hexadecimal data just by adding a prefix to the integer and adding the required range to it. In this concept, we also discussed an example code inserting the hexadecimal values to this chr () and ord() functions.


Related Topics

NSE Tools In Python

About NSE NSE (National Stock Exchange) of India Limited is the advanced stock exchange of India. It is located in Mumbai, Maharastra and It was organized in 1992. It was...

2 minutes read.

Application to get live USD/INR rate Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Python object()

Python object() class The object class in Python is a base for all classes. It returns a new featureless object. Syntax class object Parameter NA Return It returns a new featureless object. Example 1 # Python program explaining # the object()...

1 minute read.

Python 2.7 data structures

The rundown information type has a few additional techniques. Here is every one of the strategies for listing objects: list.append(x): Add a thing to the furthest limit of the rundown; comparable...

9 minutes read.

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

Iterate a Dictionary in Python – Part 2

In this tutorial, we will learn above various methods used to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to...

3 minutes read.

Python Redis Example

Introduction: Redis, representing Distant Word reference Server, is a kind of key-esteem NoSQL server. Redis is intended to help circle capacity for determination, holding information after power is stopped, and stores information...

5 minutes read.

How to Update Python?

How to Update Python In this article, we will discuss how we can update Python in our system. For a better understanding, this article will cover all the steps right from installation...

4 minutes read.

Executing Shell Commands in Python

This tutorial aims to make us understand what a Shell is, what is the importance of a Shell, what are Shell commands in Python, and how can we execute the...

3 minutes read.

Python vs Java

There are a lot of programming languages out there, and every day, a new programming language is developing in some parts of the world. Each programming language is a mixture...

5 minutes read.

Python Glob

Methods for matching files that include particular patterns in accordance with UNIX shell-related expansion rules are referred to as "glob" methods. Similar methods for finding, locating, and searching for all of...

12 minutes read.

How to append an Array in Python

A group of objects kept at adjacent memory regions is known as an array. It is a container with a set capacity for a certain number of things, all of...

7 minutes read.

How to create a class in python

In any programming language, a class is a user-defined plan or blueprint using which objects or instances of the class are created. You may wonder why we need classes in programming....

5 minutes read.

Python String isalnum() method

Python String isalnum() method The string.isalnum () method in Python returns a boolean value true if all characters in the string are alphanumeric else for any other value it returns false. Syntax String.isalnum() Parameter NA Return This...

1 minute read.

Reading a File Line by Line in Python

Introduction In this tutorial, we will learn about reading files in python line by line. Before reading the files, let us know a little information about the files first. Files A file is...

6 minutes read.

Is Python Case-sensitive when Dealing with Identifiers

Yes, Python is a case-sensitive language while dealing with identifiers. Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. It is a case-sensitive...

6 minutes read.

Best app for python

What is python? Python is a prevalent, general-purpose, and high-level programming language. Python Programming Language is appropriate for experienced programmers and also for beginners. Python programming language is being utilized by...

19 minutes read.

Python Multiprocessing Processor

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python tuple() function

Python tuple() function The tuple() function in Python creates a tuple object. Syntax tuple([iterable]) Parameter Iterable: This parameter represents a sequence, collection or an iterator object. Return This function returns a tuple object. Example 1 # Python Program explaining #...

1 minute read.

Pointers in Python

In this tutorial, we will study what pointers are and if they have any utility in python. Now, let us understand what pointers are Pointers Pointers are special variables used to store the...

3 minutes read.