Understanding Python chr()
Function
Python chr() is a built-in function that returns the character representing the specified Unicode. It takes an integer as input, representing a Unicode code point, and returns the corresponding character as a string. This function is the inverse of the ord() function, which returns the Unicode code point for a given character. You can use chr() to convert an integer to its corresponding character representation.
Syntax of Python chr
()
chr
()character = chr(integer)
Explanation
character
: Variable will store the character (as a string) returned by the function.chr()
: Built-in function that performs the conversion from integer to character.integer
: Integer representing the Unicode code point of the desired character. It is used as input for thechr()
function.
Example of Python chr
()
chr
()char = chr(65)
print(char)
Explanation
char = chr(65)
: Callschr()
with the integer 65 (which is the Unicode code point for ‘A’) and stores the result inchar
.print(char)
: Prints the value ofchar
, which is the character ‘A’.
Output
A
chr()
Parameters
Python chr() function takes only one parameter, which is an integer. This integer represents the Unicode code point of the character you want to obtain. The valid range for this integer is from 0 to 1,114,111 (0x10FFFF in hexadecimal). If you provide an integer outside this range, chr() will raise a ValueError. It’s important to note that chr() expects an integer; if you provide any other data type, it will raise a TypeError.
Example
char = chr(97)
print(char)
Explanation
char = chr(97)
: Callschr()
with the integer 97 (Unicode for ‘a’) and stores the result inchar
.print(char)
: Prints the value ofchar
, which is ‘a’.
Output
a
chr()
Return Value
Python chr() function returns a string representing the character corresponding to the Unicode code point provided as the input integer. The returned string will always have a length of 1, as it represents a single character. It’s important to remember that chr() returns a string, not the integer code point itself. If the integer passed to chr() is outside the valid Unicode range, it will raise a ValueError.
Example
char = chr(8364)
print(char)
Explanation
char = chr(8364)
: Callschr()
with 8364 (Unicode for the euro symbol) and stores the result inchar
.print(char)
: Prints the value ofchar
, which is the euro symbol ‘€’.
Output
€
How to Use Python chr()
Function
To use the Python chr() function, you call it with an integer argument representing the Unicode code point of the character you want. The function will return the corresponding character as a string. You can then store this string in a variable, print it, or use it in any other way you would use a string. It’s a straightforward way to convert an integer representation of a character back into its actual character form.
Example
char = chr(122)
print(char)
Explanation
char = chr(122)
: Callschr()
with 122 (Unicode for ‘z’) and assigns the result tochar
.print(char)
: Prints the value ofchar
, which is ‘z’.
Output
z
Python chr()
with Integer Numbers
When you use Python chr() with integer numbers, you’re asking Python to give you the character corresponding to that integer’s Unicode code point. Each character in Unicode has a unique integer code, and chr() allows you to retrieve that character. For instance, if you pass the integer 65 to chr(), it will return the string ‘A’, because 65 is the Unicode code point for the uppercase letter ‘A’. To get a valid output, you can pass any integer between 0 and 1,114,111 (inclusive).
Example
char = chr(70)
print(char)
Explanation
char = chr(70)
: Callschr()
with 70 (Unicode for ‘F’) and stores the result inchar
.print(char)
: Prints the value ofchar
, which is ‘F’.
Output
F
Python chr()
with Out of Range Integer
If you use Python chr() with an integer outside the valid Unicode range (0 to 1,114,111), Python will raise a ValueError. This is because there are no characters associated with code points outside of this range. It’s important to ensure that the integer you pass to chr() falls within the valid range to avoid this error. If you are not sure about the range, you can first check if the integer is within the valid range or not.
Example
try:
char = chr(1114112)
print(char)
except ValueError as e:
print(f"ValueError: {e}")
Explanation
try:
: This block attempts to execute the code that might raise an error.char = chr(1114112)
: Tries to callchr()
with an out-of-range integer.print(char)
: Print the character if the integer were valid.except ValueError as e:
: This block catches theValueError
if it occurs.print(f"ValueError: {e}")
: Prints the error message.
Output
ValueError: chr() arg not in range(0x110000)
Python chr()
with Non-Integer Arguments
If you try to use Python chr() with a non-integer argument, such as a string or a float, Python will raise a TypeError. The chr() function is designed to work with integers representing Unicode code points. It cannot directly handle other data types. If you have a non-integer value that you want to use with chr(), you need to convert it to an integer first.
Example
try:
char = chr("A")
print(char)
except TypeError as e:
print(f"TypeError: {e}")
Explanation
try:
: This block attempts to execute the code that might raise an error.char = chr("A")
: Tries to callchr()
with a string argument, which causes aTypeError
.print(char)
: Print the character if the argument were valid.except TypeError as e:
: This block catches theTypeError
if it occurs.print(f"TypeError: {e}")
: Prints the error message.
Output
TypeError: an integer is required (got type str)
Python chr(
) with Special Character
You can use Python chr() to get special characters, such as whitespace characters (like newline \n or tab \t) or control characters, by providing their corresponding integer Unicode code points. For example, passing the integer 10 to chr() will return the newline character (\n), and passing the integer 9 will return the tab character (\t). This allows you to work with characters that might not be easily typable or visible.
Example
newline_char = chr(10)
tab_char = chr(9)
print(f"Newline: {repr(newline_char)}")
print(f"Tab: {repr(tab_char)}")
Explanation
newline_char = chr(10)
: Gets the newline character (Unicode 10) usingchr()
.tab_char = chr(9)
: Gets the tab character (Unicode 9) usingchr()
.print(f"Newline: {repr(newline_char)}")
: Prints the newline character, usingrepr()
to show it in its escaped form.print(f"Tab: {repr(tab_char)}")
: Prints the tab character, usingrepr()
to show it in its escaped form.
Output
Newline: ‘\n’
Tab: ‘\t’
Python chr()
to Print Currency Symbol
You can use Python chr() to print currency symbols by providing their corresponding Unicode code points as integers. For instance, the euro symbol (€) has a Unicode code point of 8364. Passing this integer to chr() will return the euro symbol as a string, which you can print or use in your code. This is useful when displaying currency symbols that might not be directly available on your keyboard.
Example
euro_symbol = chr(8364)
pound_symbol = chr(163)
print(f"Euro: {euro_symbol}")
print(f"Pound: {pound_symbol}")
Explanation
euro_symbol = chr(8364)
: Gets the euro symbol (€) using its Unicode code point (8364).pound_symbol = chr(163)
: Gets the pound symbol (£) using its Unicode code point (163).print(f"Euro: {euro_symbol}")
: Prints the euro symbol.print(f"Pound: {pound_symbol}")
: Prints the pound symbol.
Output
Euro: €
Pound: £
Python chr()
to Print Emojis
You can use Python chr() to print emojis by providing their corresponding Unicode code points as integers. Emojis are characters in the Unicode standard, just like letters and numbers. Each emoji has a unique code point. For example, the “smiling face with heart-eyes” emoji 😍 has a Unicode code point of 128525. Passing this integer to chr()
will return the emoji as a string.
Example
heart_eyes_emoji = chr(128525)
thumbs_up_emoji = chr(128077)
print(f"Heart Eyes: {heart_eyes_emoji}")
print(f"Thumbs Up: {thumbs_up_emoji}")
Explanation
heart_eyes_emoji = chr(128525)
: Gets the “smiling face with heart-eyes” emoji using its Unicode code point.thumbs_up_emoji = chr(128077)
: Gets the “thumbs up” emoji using its Unicode code point.print(f"Heart Eyes: {heart_eyes_emoji}")
: Prints the heart-eyes emoji.print(f"Thumbs Up: {thumbs_up_emoji}")
: Prints the thumbs-up emoji.
Output
Heart Eyes: 😍
Thumbs Up: 👍
Python chr()
to Get the ASCII Value of Integers
You can use Python chr() to get the ASCII characters corresponding to integer values. ASCII (American Standard Code for Information Interchange) defines 128 characters, with code points from 0 to 127. Passing an integer within this range to chr() returns the corresponding ASCII character as a string. For example, chr(65) returns ‘A’, and chr(97) returns ‘a’. This method helps in converting ASCII value to its character representation.
Example
uppercase_a = chr(65)
lowercase_a = chr(97)
print(f"Uppercase A: {uppercase_a}")
print(f"Lowercase a: {lowercase_a}")
Explanation
uppercase_a = chr(65)
: Gets the uppercase ‘A’ character using its ASCII code point (65).lowercase_a = chr(97)
: Gets the lowercase ‘a’ character using its ASCII code point (97).print(f"Uppercase A: {uppercase_a}")
: Prints the uppercase ‘A’.print(f"Lowercase a: {lowercase_a}")
: Prints the lowercase ‘a’.
Output
Uppercase A: A
Lowercase a: a
Conclusion
Python chr() function is handy for working with characters and their Unicode code points. It allows you to convert an integer into its corresponding character representation, which can be helpful in various scenarios, from handling ASCII values to working with special characters, currency symbols, emojis, and integers. Understanding how Python chr handles different inputs, including integers within and outside the valid Unicode range and non-integer arguments, is essential for using it effectively and avoiding errors like ValueError or TypeError. Remember that chr() function will always return a string of length 1, which is the character representation of the integer passed to it.