In this article we will explore how to use the Python ord() and chr() functions.

Table of Contents



Introduction

Python ord() function is a built-in function that returns the Unicode code point of a specified character.

A Unicode code point is an integer that is used to represent a character in the Unicode standard.

The ord() function process is defined as:

ord(character) -> Unicode code

where character is a Unicode character.


Python chr() function is a built-in function that returns the Unicode code point of a specified character.

The chr() function process is defined as:

chr(integer) -> Unicode character

Convert a character to Unicode code point using ord()

Let’s try to use ord() function to find the Unicode code point of the letters A, B, and C:


#UCP of letter A
a = ord('A')
#UCP of letter B
b = ord('B')
#UCP of letter C
c = ord('C')

#Print values
print(a)
print(b)
print(c)

and you should get:

65
66
67

where each integer represents a Unicode character.

You can use ord() function to find the Unicode code points of other characters including special characters.


Convert a string to Unicode code points using ord()

Notice that the ord() function can take only one character as an argument, as mentioned in the introduction:

ord(character) -> Unicode code

and if you try using it with a string that has more than 1 character, you will get a TypeError:


#UCP of string
x = ord('Python')

and you should get:

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

So how can we convert the whole string to Unicode code points?

We will have to do it character by character, and there are a few ways of solving this task:

  • Using Python map() function
  • Using list comprehension

Convert a string to Unicode code points using ord() and map()

Using Python map() function we can apply Python ord() function on every element of the string:


#Define a string
py_str = 'Python'

#UCP of string
ucp_vals = list(map(ord, py_str)

#Print UCP values
print(ucp_vals)

and you should get:

[80, 121, 116, 104, 111, 110]

Convert a string to Unicode code points using ord() and list comprehension

Another way to solve this task is to use ord() function with list comprehension in Python:


#Define a string
py_str = 'Python'

#UCP of string
ucp_vals = [ord(char) for char in py_str]

#Print UCP values
print(ucp_vals)

and you should get:

[80, 121, 116, 104, 111, 110]

Convert an integer to Unicode character using chr()

You can also reverse the ord() function operation by using chr() function, which will convert a Unicode code point (in integer format) to a Unicode character.

For example, let’s see which characters the 97, 98, and 99 Unicode code points represent:


#UCP of letter A
c1 = chr(97)
#UCP of letter B
c2 = chr(98)
#UCP of letter C
c3 = chr(99)

#Print values
print(c1)
print(c2)
print(c3)

and you should get:

a
b
c

Conclusion

In this article we explored how to use the Python ord() and chr() functions.

Now that you know the basic functionality, you can practice using it with other iterable data structures for more complex use cases.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Functions tutorials.