The opposite of Python's ord() function, which returns the Unicode code point of a character, is the chr() function. The chr() function takes an integer representing a Unicode code point and returns the corresponding character.
Here's an example to illustrate the use of both ord() and chr() functions:
# Using ord() to get the Unicode code point of a character character = 'A' unicode_code_point = ord(character) print(f"Unicode code point of '{character}' is {unicode_code_point}") # Using chr() to get the character from a Unicode code point unicode_code_point = 65 # ASCII code for 'A' character = chr(unicode_code_point) print(f"Character corresponding to Unicode code point {unicode_code_point} is '{character}'") Output:
Unicode code point of 'A' is 65 Character corresponding to Unicode code point 65 is 'A'
So, while ord() converts a character to its Unicode code point, chr() converts a Unicode code point back to its corresponding character.
What Does the chr() Function Do in Python?
chr() is the opposite of ord(). It returns the character corresponding to a given Unicode code point.code_point = 97 character = chr(code_point) print(character) # Output: 'a'
Using chr() to Convert Integers to Characters
chr().code_point = 65 character = chr(code_point) print(character) # Output: 'A'
How to Convert ASCII Values to Characters in Python
chr(), where ASCII values range from 0 to 127.ascii_value = 48 character = chr(ascii_value) print(character) # Output: '0'
Converting Unicode Code Points to Characters with chr()
chr() can convert a Unicode code point to its corresponding character, supporting a wide range of characters beyond ASCII.unicode_point = 8364 # Euro sign character = chr(unicode_point) print(character) # Output: '�'
Converting Characters to Unicode Code Points and Back
ord() to convert a character to its Unicode code point and chr() to convert back.char = 'b' code_point = ord(char) print(code_point) # Output: 98 recovered_char = chr(code_point) print(recovered_char) # Output: 'b'
Looping Through Unicode Code Points and Converting with chr()
chr() to convert them to characters.for code_point in range(65, 70): print(chr(code_point)) # Outputs: 'A', 'B', 'C', 'D', 'E'
Creating a String from a List of Unicode Code Points with chr()
chr() for each code point.code_points = [72, 101, 108, 108, 111] result_string = ''.join(chr(cp) for cp in code_points) print(result_string) # Output: 'Hello'
Using chr() to Create Special Characters
\n), tab (\t), or other control characters using chr().newline_char = chr(10) # Newline tab_char = chr(9) # Tab formatted_string = f"First line{newline_char}Second line{tab_char}Indented" print(formatted_string) # Output: # First line # Second line Indented How to Convert Binary to Characters Using chr()
chr().binary = '01100001' # Binary for 'a' code_point = int(binary, 2) character = chr(code_point) print(character) # Output: 'a'
Using chr() to Convert Hexadecimal to Characters
chr().hex_value = '41' # Hexadecimal for 'A' code_point = int(hex_value, 16) character = chr(code_point) print(character) # Output: 'A'
ng2-admin merge-conflict-resolution ssis-2012 jframe windows-runtime control-structure propertynotfoundexception filter spelevaluationexception angular-ui