Remember
chr() returns the character for a Unicode code point. It is the reverse of ord() for a single character.
chr() returns the character for a Unicode code point. It is the reverse of ord() for a single character.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
code_point = 65
print(chr(code_point))Top-level scope
65chr() returns the character for a Unicode code point. It is the reverse of ord() for a single character.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
code_point = 65
print(chr(code_point))
|
v
DISPLAYED RESULT
A
FINAL BINDINGS
code_point --> object 1 (int)
object 1: 65Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
code_point = 65
print(chr(code_point))Top-level scope
65The example has finished. Compare the final output with the values and references above.
A
What do you expect this program to print?
code_point = 65
print(chr(code_point))chr() FunctionThe chr() function in Python returns the character that represents the specified Unicode code point (an integer). It’s the opposite of ord(), which gives the Unicode number for a character.
chr(i)
i – An integer representing the Unicode code point (between 0 and 1,114,111)iprint(chr(65))
A
This is because the Unicode code point 65 maps to the character 'A'.
print(chr(97))
a
print(chr(36)) # Dollar sign
print(chr(8364)) # Euro sign
$
€
for i in range(65, 91):
print(chr(i), end=" ")
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
ValueErrorTypeErrorThe chr() and ord() functions often appear together in string manipulation or encryption interview problems.
chr(i) converts a Unicode code point to a characterord(), which converts a character to a numberWrite a Python program to print all characters from Unicode 97 to 122 on one line (a to z).
for i in range(97, 123):
print(chr(i), end=" ")
Connect what you noticed in the example with the idea below.
chr() returns the character for a Unicode code point. It is the reverse of ord() for a single character.
chr() returns the decimal digits of its argument.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.