Python does not have a character data type. Instead, it has a string data type, and you can represent a character as a string with length 1.
When it comes to converting an integer (int) to a character (char), it involves transforming a numeric value into its corresponding character representation, typically using ASCII or Unicode mappings.
Python provides a built-in way called the “chr()” function to convert an integer to a character. It accepts an integer argument and returns a string representing a character at that Unicode code point.
Conversion using ASCII/Unicode
my_int = 70 print(my_int) # Output: 70 print(type(<meta charset='utf-8'>my_int)) # Output: <class 'int'> # Convert integer to character my_char = chr(my_int) print(my_char) print(type(my_char)) print(chr(65)) # Output: A print(chr(97)) # Output: a
List of integers to characters
What if we have a list of integers and we want to convert each element to its corresponding character type:
list_with_char_codes = [69, 72, 78, 81, 90, 99] for number in list_with_char_codes: character = chr(number) print("Character of ASCII value", number, "is", character) # Output: # Character of ASCII value 69 is E # Character of ASCII value 72 is H # Character of ASCII value 78 is N # Character of ASCII value 81 is Q # Character of ASCII value 90 is Z # Character of ASCII value 99 is c
Error handling
If you pass an invalid code point to chr(), it will raise a ValueError.
try: print(chr(1114112)) # Out of range except ValueError as e: print(e) # Output: ValueError: chr() arg not in range(0x110000)
In the above code, you can see that we used the try/except mechanism to handle the potential ValueError, which prevents the program from crashing.
Convert an Integer to a String Character (e.g., 5 to ‘5’)
In this case, we will convert an integer to its string representation. For example, 5 will be converted to “5”. For that, we can use the “str()” function or “string formatting”.
Note that we are not converting to its ASCII value here.
int_value = 9 print(type(int_value)) # Output: <class 'int'> print(str(int_value)) # Output: "9" print(type(str(int_value))) # Output: <class 'str'> int_value = 123 print(type(int_value)) # Output: <class 'int'> print(str(int_value)) # Output: "123" print(type(str(int_value))) # Output: <class 'str'>
The above program shows that it returns a single-character string for a single integer and a multi-character string for multi-digit integers.
Handling negative integers
Negative integers don’t directly convert to an ASCII value, but you can convert them into a string representation using the str() function.
# Handle negative integers int_value = -72 try: print(chr(int_value)) # Output: ValueError: chr() arg not in range(0x110000) except ValueError as e: print(e) # Alternative: Convert to string print(str(int_value)) # Output: -72
We got the ValueError because the chr() function does not accept a negative integer, as Unicode code points are non-negative. For string conversion, str() includes the negative sign (e.g., -72 becomes “-72”).