Home
Map
ord, chr Built InsUse the ord and chr built-in methods. These convert characters to integers and back again.
Python
This page was last reviewed on May 24, 2023.
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.
letters = "ABCabc123" for letter in letters: # Get number that represents letter in ASCII. number = ord(letter) print(letter, "=", number)
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.
numbers = [97, 98, 99] for number in numbers: # Convert ASCII-based number to character. letter = chr(number) print(number, "=", letter)
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.
# 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)
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.
ROT13
Detail With maketrans and translate() we can translate strings. We can use chr and ord to build translation strings for these methods.
String translate
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.