A log stores character identifiers as integers and must display their symbols.
What You Need to Understand
The built-in chr(number) returns the Unicode character with a given valid code point. It is the reverse operation of ord() for valid values.
When to Use It
A log stores character identifiers as integers and must display their symbols.
Step-by-Step Python Example
print(chr(65))
print(chr(8364))
print(chr(0x1F600))
How the Example Works
The values produce A, €, and a grinning face. Hexadecimal integers are still ordinary integers.
Example Output
A
€
😀
Py3
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
codes = [80, 121, 51]
print("".join(chr(code) for code in codes))
Important Behavior and Edge Cases
Values outside the valid Unicode range raise ValueError. Printing a character also depends on terminal font support.
Try It Yourself
Convert the integers 72, 105, and 33 into a short greeting.
Common Beginner Mistakes
- Values outside the valid Unicode range raise
ValueError. Printing a character also depends on terminal font support. - Applying a string method before defining whether text is intended for display, comparison, parsing, or input validation.
Key Takeaways
- The built-in
chr(number)returns the Unicode character with a given valid code point. - The values produce
A,€, and a grinning face. - Values outside the valid Unicode range raise
ValueError.