The chr() function in Python is a built-in function that allows you to convert an integer (representing the Unicode code point of a character) into its corresponding string representation. This function is commonly used in various scenarios, such as encoding and decoding of text, and can help you manipulate strings effectively by working with individual characters.
1. Introduction
The primary purpose of the chr() function is to return a string representing a character whose Unicode code point is the integer value passed to it. Understanding this function is crucial for beginners, as it lays the groundwork for dealing with character data in Python.
2. Syntax
The syntax of the chr() function is straightforward. Here’s how it looks:
chr(i)
Where:
- i – An integer representing a valid Unicode code point, ranging from 0 to 1114111 (inclusive).
3. Return Value
The chr() function returns:
- A string of a single character corresponding to the Unicode code point passed in.
The data type of the return value is str. This means that the return value is a string consisting of one character.
4. Example
Below are some simple examples demonstrating the usage of the chr() function. Let’s look at each example with a step-by-step breakdown.
Example 1: Using chr() with Basic Unicode Values
print(chr(65)) # Output: A
print(chr(97)) # Output: a
Explanation:
- The first print function converts 65 to the character ‘A’ (uppercase). This is because ‘A’ is the Unicode character that corresponds to the code point 65.
- The second print function converts 97 to ‘a’ (lowercase).
Example 2: Working with Special Characters
print(chr(36)) # Output: $
print(chr(9731)) # Output: ☃
Explanation:
- Here, the first function call converts the code point 36 into the symbol ‘$’, which is the dollar sign.
- The second function call converts 9731 into a Unicode snowman character ‘☃’ (U+2603).
5. Related Functions
A closely related function to chr() is the ord() function. While the chr() function converts code points to characters, the ord() function does the opposite: it converts a single character to its corresponding Unicode code point.
Here’s a quick comparison:
Function | Purpose | Example |
---|---|---|
chr() | Convert an integer to a character | chr(66) results in ‘B’ |
ord() | Convert a character to an integer | ord('B') results in 66 |
6. Conclusion
The chr() function in Python is an essential tool for anyone working with strings and character data. It allows you to convert Unicode code points into their corresponding characters, enabling you to manipulate such data easily. Coupled with the ord() function, you have a powerful complementary pair that can assist in various text processing tasks.
FAQs
- Q: What happens if I pass an integer outside the valid range to chr()?
- A: If you pass an integer outside the range of 0 to 1114111, a ValueError will be raised.
- Q: Can chr() handle non-UTF-8 characters?
- A: The chr() function works with Unicode, so as long as the code point is valid, it will produce the corresponding character regardless of encoding.
- Q: Is chr() supported in earlier versions of Python?
- A: Yes, the chr() function has been available since the early versions of Python (2.0 and above).
Leave a comment