Use ord() and chr() to Convert Characters and Integers
In this step, we will learn how to use the built-in Python functions ord() and chr() to convert between characters and their corresponding integer representations in Unicode.
In Python 3, strings are represented using Unicode. The ord() function takes a single character as input and returns its corresponding Unicode decimal integer value.
Let's create a new Python file to experiment with these functions. In the WebIDE, right-click on the project directory in the file explorer and select New File. Name the file char_conversion.py.
Open char_conversion.py in the editor and add the following code:
## Use ord() to get the Unicode decimal value of characters
char1 = 'a'
char2 = 'é'
char3 = ';'
print(f"The Unicode decimal value of '{char1}' is: {ord(char1)}")
print(f"The Unicode decimal value of '{char2}' is: {ord(char2)}")
print(f"The Unicode decimal value of '{char3}' is: {ord(char3)}")
Save the file by pressing Ctrl + S (or Cmd + S on macOS).
Now, open the integrated terminal again (if it's not already open) and run the script using the python command:
python char_conversion.py
You should see output similar to this:
The Unicode decimal value of 'a' is: 97
The Unicode decimal value of 'é' is: 233
The Unicode decimal value of ';' is: 59
The chr() function performs the reverse operation. It takes a decimal integer (or a hexadecimal integer) representing a Unicode code point and returns the corresponding character.
Let's add more code to char_conversion.py to use the chr() function. Append the following lines to the existing code:
## Use chr() to get the character from a Unicode decimal value
int1 = 8364
int2 = 8482
print(f"The character for Unicode decimal value {int1} is: {chr(int1)}")
print(f"The character for Unicode decimal value {int2} is: {chr(int2)}")
## You can also use hexadecimal values with chr()
hex_int = 0x00A9 ## Hexadecimal for the character '©'
print(f"The character for Unicode hexadecimal value {hex(hex_int)} is: {chr(hex_int)}")
Save the file again.
Run the script from the terminal:
python char_conversion.py
The output should now include the results from the chr() function:
The Unicode decimal value of 'a' is: 97
The Unicode decimal value of 'é' is: 233
The Unicode decimal value of ';' is: 59
The character for Unicode decimal value 8364 is: €
The character for Unicode decimal value 8482 is: ™
The character for Unicode hexadecimal value 0xa9 is: ©
You might wonder how to find the hexadecimal Unicode representation of a character. You can use the ord() function to get the decimal value and then the built-in hex() function to convert the decimal value to its hexadecimal string representation.
Add the following code to char_conversion.py:
## Convert a character to its hexadecimal Unicode representation
char_copyright = '©'
decimal_copyright = ord(char_copyright)
hexadecimal_copyright = hex(decimal_copyright)
print(f"The hexadecimal Unicode value of '{char_copyright}' is: {hexadecimal_copyright}")
Save the file and run it one last time:
python char_conversion.py
The final output will include the hexadecimal value for the character '©':
The Unicode decimal value of 'a' is: 97
The Unicode decimal value of 'é' is: 233
The Unicode decimal value of ';' is: 59
The character for Unicode decimal value 8364 is: €
The character for Unicode decimal value 8482 is: ™
The character for Unicode hexadecimal value 0xa9 is: ©
The hexadecimal Unicode value of '©' is: 0xa9
This demonstrates how ord(), chr(), and hex() can be used together to work with character encodings in Python.