Purpose
Converts a character to the appropriate ASCII or Unicode code, or vice versa.
Method
For ASCII codes (0~255 range)
The code is as follows:
>>> Print ord (' A ')
65
>>> Print Chr (65)
A
For Unicode characters, note that only Unicode characters of length 1 are received
The code is as follows:
>>> Print ord (U ' \u54c8 ')
21704
>>> print UNICHR (21704)
Ha
>>> Print repr (UNICHR (21704))
U ' \u54c8 '
Chr () differs from STR (), a value that receives only 0~255 returns the character that corresponds to the ASCII value, and one that accepts any type of return string format
The code is as follows:
>>> chr (97)
A
>>> STR (97)
' 97 '
Use map and the above functions to get a list of character values or code values
The code is as follows:
>>> Print Map (ord, (U ' \u54c8 ', U ' \u54c9 '))
[21704, 21705]
>>> Print Map (Unichr,range (21704,21707))
[u ' \u54c8 ', U ' \u54c9 ', U ' \u54ca ']