- Python chr() and ord()
- The chr() function
- Syntax
- The ord() function
- Syntax
- Passing Hexadecimal Data
- Conclusion
- References
- Python ord(), chr() functions
- Python ord()
- Python chr()
- Python Ord and Chr Functions: Working with Unicode
- What is Unicode and How is it Used in Python?
- Python ord() Function: Unicode to Integer
- Passing Multiple Characters into the Python ord() Function
- Using the Python ord() for Multiple Characters
- Python chr() Function: Integer to Unicode Character
- Converting Multiple Integers to Strings Using Python chr()
- Working with Hexadecimal Data in Python Ord and Chr
- Conclusion
- Additional Resources
Python chr() and ord()
Python’s built-in function chr() is used for converting an Integer to a Character, while the function ord() is used to do the reverse, i.e, convert a Character to an Integer.
Let’s take a quick look at both these functions and understand how they can be used.
The chr() function
Syntax
This takes in an integer i and converts it to a character c , so it returns a character string.
Here is an example to demonstrate the same:
# Convert integer 65 to ASCII Character ('A') y = chr(65) print(type(y), y) # Print A-Z for i in range(65, 65+25): print(chr(i), end = " , ")
A 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
The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in Hexadecimal). ValueError will be raised if the integer i is outside that range.
Let’s verify that with some examples
This will raise a ValueError .
ValueError: chr() arg not in range(0x110000)
start = 0 end = 1114111 try: for i in range(start, end+2): a = chr(i) except ValueError: print("ValueError for i =", i)
ValueError for i = 1114112
The ord() function
The ord() function takes a string argument of a single Unicode character and returns its integer Unicode code point value. It does the reverse of chr() .
Syntax
This takes a single Unicode character (string of length 1) and returns an integer, so the format is:
To verify that it does the reverse of chr() , let us test the function using some examples.
# Convert ASCII Unicode Character 'A' to 65 y = ord('A') print(type(y), y) alphabet_list = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # Print 65-90 for i in alphabet_list: print(ord(i), end = " , ")
65 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 , 83 , 84 , 85 , 86 , 87 , 88 , 89 , 90 ,
This raises a TypeError if the length of the input string is not equal to one.
TypeError: ord() expected a character, but string of length 2 found
Passing Hexadecimal Data
We can also pass Integers represented in other common bases, such as Hexadecimal format (base 16) to chr() and ord().
In Python, we can use Hexadecimal by prefixing an integer with 0x , provided it is within the 32/64 bit range for integer values.
>>> print(hex(18)) '0x12' >>> print(chr(0x12)) '\x12' >>> print(ord('\x12')) 18 >>> print(int('\x12')) 18
We pass the integer 18 in hexadecimal format to chr() , which returns a hexadecimal 0x12 . We pass that to chr() and use ord() to get back our integer.
Note that we could also get the integer using int() , since a single character string is also a string, which can be a valid parameter to the above function.
Conclusion
In this article, we learned about using chr() and ord() to convert Integers to Characters and vice-versa.
References
Python ord(), chr() functions
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Python ord() and chr() are built-in functions. They are used to convert a character to an int and vice versa. Python ord() and chr() functions are exactly opposite of each other.
Python ord()
Python ord() function takes string argument of a single Unicode character and return its integer Unicode code point value. Let’s look at some examples of using ord() function.
x = ord('A') print(x) print(ord('ć')) print(ord('ç')) print(ord('$'))
Python chr()
Python chr() function takes integer argument and return the string representing a character at that code point.
y = chr(65) print(y) print(chr(123)) print(chr(36))
Since chr() function takes an integer argument and converts it to character, there is a valid range for the input. The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in hexadecimal format). ValueError will be raised if the input integer is outside that range.
ValueError: chr() arg not in range(0x110000)
Let’s see an example of using ord() and chr() function together to confirm that they are exactly opposite of another one.
print(chr(ord('ć'))) print(ord(chr(65)))
That’s all for a quick introduction of python ord() and chr() functions. You can checkout complete python script and more Python examples from our GitHub Repository. Reference: Official Documentation — ord, Official Documentation — chr
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about us
Python Ord and Chr Functions: Working with Unicode
In this tutorial, you’ll learn how to use the Python ord and chr functions to allow you to work better with Unicode. The Unicode standard has replaced many confusing encodings and is used to represent each character (including emojis) with a number.
The Python ord() function converts a character into an integer that represents the Unicode code of the character. Similarly, the chr() function converts a Unicode code character into the corresponding string.
You’ll learn a quick refresher on Unicode and how string characters can be represented in different ways in Python. Then, you’ll learn how the chr() and ord() functions work in Python, both for single characters and for multiple characters.
What is Unicode and How is it Used in Python?
Before diving into the ord and chr functions, let’s start off by covering why learning about Unicode is so important. At a basic level, computers work with numbers – because of this, the characters and letters that appear on a screen are numbers under the hood.
In the past, many different types of encoding existed. However, many of these were incomplete when considering the vast variety of characters that exist on the internet. In order to resolve this, the Unicode Consortium standardized specifications of how to represent characters in 1991.
The Unicode standard assigned numerical values to every type of character, from letters to symbols to emojis. The standard allowed computers to have a much easier time understanding symbols, especially as more and more symbols are being added to the internet.
The image below shows how a string character can be converted to its numeric, Unicode representation. This even includes emojis! The Unicode standard includes values for Unicode numbers ranging from 0 through 1,114,111.
Python ord() Function: Unicode to Integer
The Python ord() function is used to convert a single Unicode character into its integer representation. We can pass in any single string character and the function will return an integer.
Let’s see what this looks like:
# Converting Unicode to Int Using ord() character = 'd' print(ord(character)) # Returns: 100
We can see the integer representation of the Unicode letter ‘d’ is 100. The function ord() works by taking a single character as its input, the character that you want to convert to an integer.
Passing Multiple Characters into the Python ord() Function
The Python ord() function allows you to only pass in a single character. Let’s see what happens when we pass in more than one character:
# Converting Unicode to Int Using ord() character = 'datagy' print(ord(character)) # Raises: TypeError: ord() expected a character, but string of length 6 found
We can see that passing in more than one character into the ord() function raises a TypeError . This happens because the function expects only a single character to be passed in.
Using the Python ord() for Multiple Characters
In order to resolve the TypeError that is raised when more than one character is passed into the ord() function, we need to iterate over each character in the string. Because Python strings are iterable objects, we can directly iterate over these string values.
Let’s see how we can repeat our earlier example without raising a TypeError :
# Converting Multiple Characters into Integers word = 'datagy' unicode = [] for letter in word: unicode.append(ord(letter)) print(unicode) # Returns: # [100, 97, 116, 97, 103, 121]
We can now see the integer representation of our Unicode string. This was accomplished by looping over each letter in the string and applying the ord() function to it.
In the next section, you’ll learn about the reverse of this function: the chr() function, which allows you to pass in a Unicode number and return a string representation.
Python chr() Function: Integer to Unicode Character
The Python chr() function converts an integer representation into its corresponding Unicode string character. We can pass in any single integer and the function will return a string character.
The Python chr function does the opposite of the ord function, returning a Unicode character when an integer value is passed in.
Let’s convert an integer value into its Unicode counterpart:
# Convert an Integer into a Unicode String number = 100 unicode = chr(number) print(unicode) # Returns: d
We can see that by passing in an integer, a string character is returned.
Converting Multiple Integers to Strings Using Python chr()
Let’s try to convert some values from integers into their Unicode counterparts:
# Converting Integers to their Unicode Equivalent numbers = [100, 97, 116, 97, 103, 121] for number in numbers: print(chr(number)) # Returns: # d # a # t # a # g # y
We can go further and convert this list of numbers into an actual Python string. We can do this by using the .join() method. Let’s take a look at how this works. Rather than using a for loop, let’s use a Python list comprehension to simplify this process:
# Converting Integers to their Unicode Equivalent numbers = [100, 97, 116, 97, 103, 121] word = ''.join([chr(number) for number in numbers]) print(word) # Returns: datagy
In the example above, we used the «».join() method to convert our resulting list of strings to a single string.
As of the writing of this article, the function accepts any value between 0 and 1,114,111, representing all the available Unicode characters. If a value outside of this range is passed into the function, the function will raise a ValueError . Let’s see what this looks like:
# Raising a ValueError with chr chr(1114112) # Raises: ValueError: chr() arg not in range(0x110000)
We can see that, as expected, a ValueError was raised.
Working with Hexadecimal Data in Python Ord and Chr
In Python, Hexadecimal numbers are numbers represented in other common bases. The hexadecimal format changes the base to 16 and can be used with both the chr and ord functions. In Python, these numbers can be used by prefixing the integer with 0x .
We can convert an integer into its hexadecimal equivalent by using the hex function. Let’s give this a shot:
# Converting an Integer to a Hexadecimal Number number = 100 hex_number = hex(100) print(hex_number) # Returns: 0x64
Now that we have the hexadecimal value for the number 100, we can pass this into the chr function to convert it to its Unicode representation:
# Converting Hexadecimal to Unicode print(chr(0x64)) # Returns: d
Here we can see that the 0x64 is a valid number representation in Python. Python will interpret the 0x prefix to represent hexadecimal formats and will convert the value into its Unicode representation.
Conclusion
In this tutorial, you learned how to work with the chr and ord functions in Python. These functions allow you to translate unicode to string characters and string characters to unicode. You also learned how to use the ord() function for multiple characters. Finally, you learned how to work with hexadecimal data in Python, when using the chr and ord functions.
To learn more about the Python ord and chr functions, check out the official documentation here.
Additional Resources
To learn more about related topics, check out these tutorials: