Python ord() Function

The Python ord() function takes a character (=string of length one) as an input and returns the Unicode number of this character. For example, ord('a') returns the Unicode number 97. The inverse function of ord() is the chr() function, so chr(ord('a')) returns the original character 'a'.

Here are three examples of passed Unicode characters transformed into their Unicode numbers using the ord() built-in function:

>>> ord('A')
65
>>> ord('B')
66
>>> ord('€')
8364

The syntax is very straightforward:

Syntax: ord(c)
Argumentstring cA Unicode string of length one. If you pass a longer string with multiple characters, it’ll throw a TypeError.
Return ValueintReturns the Unicode number associated to the Unicode symbol c using a Unicode table lookup.

But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners

Python ord() Visual Explanation

Unicode Table

Here’s a small part of the massive Unicode table that maps each Unicode symbol to a decimal number:

Unicode characterDescriptionDec
Alatin capital letter a65
Blatin capital letter b66
Clatin capital letter c67
Dlatin capital letter d68
Elatin capital letter e69
Flatin capital letter f70
Glatin capital letter g71
Hlatin capital letter h72
Ilatin capital letter i73
Jlatin capital letter j74
Klatin capital letter k75
Llatin capital letter l76
Mlatin capital letter m77
Nlatin capital letter n78
Olatin capital letter o79
Platin capital letter p80
Qlatin capital letter q81
Rlatin capital letter r82
Slatin capital letter s83
Tlatin capital letter t84
Ulatin capital letter u85
Vlatin capital letter v86
Wlatin capital letter w87
Xlatin capital letter x88
Ylatin capital letter y89
Zlatin capital letter z90
alatin small letter a97
blatin small letter b98
clatin small letter c99
dlatin small letter d100
elatin small letter e101
flatin small letter f102
glatin small letter g103
hlatin small letter h104
ilatin small letter i105
jlatin small letter j106
klatin small letter k107
llatin small letter l108
mlatin small letter m109
nlatin small letter n110
olatin small letter o111
platin small letter p112
qlatin small letter q113
rlatin small letter r114
slatin small letter s115
tlatin small letter t116
ulatin small letter u117
vlatin small letter v118
wlatin small letter w119
xlatin small letter x120
ylatin small letter y121
zlatin small letter z122
Àlatin capital letter a with grave192
Álatin capital letter a with acute193
Âlatin capital letter a with circumflex194
Ãlatin capital letter a with tilde195
Älatin capital letter a with diaeresis196
Ålatin capital letter a with ring above197
Ælatin capital letter ae198
Çlatin capital letter c with cedilla199
Èlatin capital letter e with grave200
Élatin capital letter e with acute201
Êlatin capital letter e with circumflex202
Ëlatin capital letter e with diaeresis203
Ìlatin capital letter i with grave204
Ílatin capital letter i with acute205
Îlatin capital letter i with circumflex206
Ïlatin capital letter i with diaeresis207
Ðlatin capital letter eth208
Ñlatin capital letter n with tilde209
Òlatin capital letter o with grave210
Ólatin capital letter o with acute211
Ôlatin capital letter o with circumflex212
Õlatin capital letter o with tilde213
Ölatin capital letter o with diaeresis214
Ølatin capital letter o with stroke216
Ùlatin capital letter u with grave217
Úlatin capital letter u with acute218
Ûlatin capital letter u with circumflex219
Ülatin capital letter u with diaeresis220
Ýlatin capital letter y with acute221
Þlatin capital letter thorn222
ßlatin small letter sharp s223
àlatin small letter a with grave224
álatin small letter a with acute225
âlatin small letter a with circumflex226
ãlatin small letter a with tilde227
älatin small letter a with diaeresis228
ålatin small letter a with ring above229
ælatin small letter ae230
çlatin small letter c with cedilla231
èlatin small letter e with grave232
élatin small letter e with acute233
êlatin small letter e with circumflex234
ëlatin small letter e with diaeresis235
ìlatin small letter i with grave236
ílatin small letter i with acute237
îlatin small letter i with circumflex238
ïlatin small letter i with diaeresis239
ðlatin small letter eth240

By passing the Unicode string from the first column into the ord() function, you obtain the associated Unicode number in the third column. Go ahead, try it yourself!

Tool: Integer to Unicode in Python

How to convert a Unicode symbol to an integer Unicode number in Python? Use the ord(c) function and pass the integer number as an argument!

Exercise: Try to obtain the Unicode number associated to the Unicode symbol ð by changing the code in the interactive code shell!

How to Convert a Unicode Integer to a String?

To convert a Unicode number i to the associated Unicode symbol, use the chr(i) function. For example, the result of chr(65) is the Unicode symbol 'A'. The inverse function is the ord(x) that converts Unicode symbol 'A' back to integer 65.

>>> chr(65)
'A'

How to Convert a Unicode Symbol to an Integer?

To convert a Unicode symbol x to the associated Unicode integer number, use the ord(x) function. For example, the result of ord('A') is the Unicode integer 65. The inverse function is the chr(i) that converts Unicode integer 65 back to Unicode symbol 'A'.

>>> ord('A')
65

Summary

The Python ord() function takes a character (=string of length one) as an input and returns the Unicode number of this character.

For example, ord('a') returns the Unicode number 97.

>>> ord('a')
97

The inverse function of ord() is the chr() function, so chr(ord('a')) returns the original character 'a'.

>>> chr(ord('a'))
'a'


Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!