The chr()
function in Python is a built-in function that takes an integer as an argument and returns the corresponding Unicode character in the form of a string of length 1.
The chr()
function is the opposite of the ord()
function, which takes a Unicode character as an argument and returns its corresponding integer value. Together, these two functions provide a convenient way to convert between integers and Unicode characters.
Syntax of the chr()
function
The syntax of the chr()
function is as follows:
chr(i)
Here, i
is an integer that represents the Unicode character.
Unicode Character Range
The integer i
must be in the range of [0, 1114111] (0x0 to 0x10FFFF), which represents the full range of Unicode characters, including special characters and non-printable ones.
Error Handling
If the integer i
is outside the valid range, the chr()
function will raise a ValueError
. It is important to handle this error in your code.
Examples of using the chr()
function
Example 1: Convert an integer to its corresponding Unicode character
print(chr(65)) # Output: A
In this example, we pass the integer value 65 to the chr()
function, which returns the Unicode character A
.
Example 2: Convert a list of integers to Unicode characters
lst = [65, 66, 67]
for i in lst:
print(chr(i))
In this example, we have a list of integers [65, 66, 67]
that represent the Unicode characters A
, B
, and C
. We use a for
loop to iterate over the list and pass each integer to the chr()
function to get the corresponding Unicode character.
Example 3: Convert a range of integers to Unicode characters
for i in range(65, 70):
print(chr(i))
In this example, we use the range()
function to generate a sequence of integers from 65 to 69. We then use a for
loop to iterate over the sequence and pass each integer to the chr()
function to get the corresponding Unicode character.
Example 4: Convert a string of integers to Unicode characters
s = "65 66 67"
lst = s.split()
for i in lst:
print(chr(int(i)))
In this example, we have a string of integers "65 66 67"
that represent the Unicode characters A
, B
, and C
. We use the split()
method to split the string into a list of integers [65, 66, 67]
. We then use a for
loop to iterate over the list and pass each integer to the chr()
function to get the corresponding Unicode character.
Example 5: Convert a list of integers to a string of Unicode characters
lst = [65, 66, 67]
s = ''.join(chr(i) for i in lst)
print(s) # Output: ABC
In this example, we have a list of integers [65, 66, 67]
that represent the Unicode characters A
, B
, and C
. We use a list comprehension with the chr()
function to create a new list of Unicode characters. We then use the join()
method to join the list of characters into a single string.
Handling ValueError
If an integer is out of the Unicode character range, we can use exception handling to prevent the program from crashing:
try:
print(chr(1114112)) # This would cause a ValueError
except ValueError as ve:
print(f"Invalid input: {ve}")
In this example, we use a try-except
block to catch the ValueError
exception if the integer is not a valid Unicode code point.
Conclusion
The chr()
function in Python is a powerful tool that makes it easy to convert an integer into its corresponding Unicode character. This function is useful in many different programming tasks, including text processing, data analysis, and web development. By mastering the chr()
function and understanding error handling, you can improve your Python programming skills and become a more effective developer.