How to Convert Character to Unicode in Python

The ord() is a built-in Python function that accepts a string of length one as an argument and returns the Unicode code point representation of the passed argument. 

Python ord

Python ord() is a built-in function that returns the number representing the Unicode code of a specified character. The ord() method returns the integer representing the Unicode code point for the given Unicode character.

 For example, ord(‘B’) returns 66, a Unicode code point value of character ‘B.’ The ord() method is the inverse of the chr() function.

See the following syntax.


The parameter is any character whose length is 1.

See the following example.


# code point of integer

# code point of alphabet 

# code point of character

See the output.

➜  pyt python3
➜  pyt

If the string length is more than one, then the TypeError will be raised.

See the following example of TypeError.



See the output.

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

So, we got the TypeError. That means we do not need to pass the string of length of 2 or more.

The Unicode code point is given the meaning by the Unicode standard, which is a number.

Code points of numbers 0-10 by using a range

See the following code example.


for n in range(10):

    print("Unicode code point of", n, '=', ord(str(n)))

See the output.

➜  pyt python3
Unicode code point of 0 = 48
Unicode code point of 1 = 49
Unicode code point of 2 = 50
Unicode code point of 3 = 51
Unicode code point of 4 = 52
Unicode code point of 5 = 53
Unicode code point of 6 = 54
Unicode code point of 7 = 55
Unicode code point of 8 = 56
Unicode code point of 9 = 57
➜  pyt

As range items are numbers, it will produce an error if directly used in the ord() function. As such, ord() takes a string, so the str() function is used for converting the number into the string.

Dynamic character example

See the following code example.


c = input("Enter a character: ")

data = ord(c)

print("The Unicode code point of the character is: ", c, "=", data)

See the output.

➜  pyt python3
Enter a character: E
The Unicode code point of the character is:  E = 69
➜  pyt

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.