Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python chr() and ord()

Python’s built-in function chr() is used for converting an Integer to a Character, while the function ord() is used to do the reverse, i.e, convert a Character to an Integer.

Let’s take a quick look at both these functions and understand how they can be used.


The chr() function

Syntax

This takes in an integer i and converts it to a character c, so it returns a character string.

Format:

c = chr(i)

Here is an example to demonstrate the same:

# Convert integer 65 to ASCII Character ('A')
y = chr(65)
print(type(y), y)

# Print A-Z
for i in range(65, 65+25):
    print(chr(i), end = " , ")

Output

<class 'str'> A
A , B , C , D , E , F , G , H , I , J , K , L , M , N , O , P , Q , R , S , T , U , V , W , X , Y , Z 

The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in Hexadecimal). ValueError will be raised if the integer i is outside that range.

Let’s verify that with some examples

print(chr(-1))

This will raise a ValueError.

ValueError: chr() arg not in range(0x110000)
start = 0
end = 1114111

try:
    for i in range(start, end+2):
        a = chr(i)
except ValueError:
    print("ValueError for i =", i)

Output

ValueError for i = 1114112

The ord() function

The ord() function takes a string argument of a single Unicode character and returns its integer Unicode code point value. It does the reverse of chr().

Syntax

This takes a single Unicode character (string of length 1) and returns an integer, so the format is:

i = ord(c)

To verify that it does the reverse of chr(), let us test the function using some examples.

# Convert ASCII Unicode Character 'A' to 65
y = ord('A')
print(type(y), y)

alphabet_list = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

# Print 65-90
for i in alphabet_list:
    print(ord(i), end = " , ")

Output

<class 'int'> 65
65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 , 83 , 84 , 85 , 86 , 87 , 88 , 89 , 90 , 

This raises a TypeError if the length of the input string is not equal to one.

y = ord('Hi')

Output

TypeError: ord() expected a character, but string of length 2 found

Passing Hexadecimal Data

We can also pass Integers represented in other common bases, such as Hexadecimal format (base 16) to chr() and ord().

In Python, we can use Hexadecimal by prefixing an integer with 0x, provided it is within the 32/64 bit range for integer values.

>>> print(hex(18))
'0x12'
>>> print(chr(0x12))
'\x12'
>>> print(ord('\x12'))
18
>>> print(int('\x12'))
18

We pass the integer 18 in hexadecimal format to chr(), which returns a hexadecimal 0x12. We pass that to chr() and use ord() to get back our integer.

Note that we could also get the integer using int(), since a single character string is also a string, which can be a valid parameter to the above function.


Conclusion

In this article, we learned about using chr() and ord() to convert Integers to Characters and vice-versa.


References


Leave a Reply

Your email address will not be published. Required fields are marked *