Python ord, chr Built Ins

This Python article covers the ord and chr built-in methods. These convert characters to integers and back again.
Ord, chr. Think of letters like A, B and C. These are parts of words. But they have an underlying representation. A sequence of bits that represents that letter.
Characters. In Python characters are represented by numbers. Often we use a one-character string. With ord we convert this into an integer. With chr, an integer into a string.
Ord example. In this program, each "letter" is a one-character string. We call ord and the result in an integer. We can do things like multiply or add that integer.

Note: The letters A, B and C have sequential numbers. This is how they are stored in ASCII.

Note 2: The number strings 1, 2 and 3 are made of characters that are represented in ASCII by 49, 50 and 51.

Python program that uses ord built-in letters = "ABCabc123" for letter in letters: # Get number that represents letter in ASCII. number = ord(letter) print(letter, "=", number) Output A = 65 B = 66 C = 67 a = 97 b = 98 c = 99 1 = 49 2 = 50 3 = 51
Chr example. Next we consider the chr built-in function. This does the opposite of ord. It converts a number (an integer) into a one-character string.

Here: We convert 97 back into the lowercase "a." This code could be used to round-trip letters and digits.

Python program that uses chr numbers = [97, 98, 99] for number in numbers: # Convert ASCII-based number to character. letter = chr(number) print(number, "=", letter) Output 97 = a 98 = b 99 = c
Chr, translate. Let's do something really crazy. Here we create strings with chr in for-loops for use as the mapped keys and values of maketrans. We then translate the strings.

So: We generate translation keys and values with an algorithm, and then apply the generated table to translate strings.

Python program that uses chr, maketrans, translate # Generate string for translation. initial = "" for i in range(97, 97 + 26): initial += chr(i) # Generate mapped chars for string. translated = "" for i in range(97, 97 + 26): translated += chr(i - 10) print("INITIAL ", initial) print("TRANSLATED", translated) # Create a lookup table. table = str.maketrans(initial, translated) # Translate this string. value = "thank you for visiting" result = value.translate(table) print("BEFORE", value) print("AFTER ", result) Output INITIAL abcdefghijklmnopqrstuvwxyz TRANSLATED WXYZ[\]^_`abcdefghijklmnop BEFORE thank you for visiting AFTER j^Wda oek \eh l_i_j_d]
ROT13. The simplest examples above would not be found in real programs. But algorithms like ROT13, which are used sometimes in programs, also can be implemented with ord and chr.

Also: Ord and chr built-ins help transform characters and strings. But they are not ideal for all translations.


Translate: With maketrans and translate() we can translate strings. We can use chr and ord to build translation strings for these methods.

A summary. Chr and ord are something we need to know to effectively use Python. There is a difference between an integer and a one-character string.
We must convert to an integer with ord—and back to a string with chr. We can change characters by adding or subtracting offset values.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls