This article describes how to convert character strings between ASCII or Unicode values, for more information, see
Purpose
Converts a character to an ASCII or Unicode code, or vice versa.
Method
For ASCII codes (0 ~ Range: 255)
The code is as follows:
>>> Print ord ('A ')
65
>>> Print chr (65)
A
For Unicode characters, note that only one Unicode character is received.
The code is as follows:
>>> Print ord (u' \ u54c8 ')
21704
>>> Print unichr (21704)
Ha
>>> Print repr (unichr (21704 ))
U' \ u54c8'
Difference between chr () and str (), one receives only 0 ~ A value of 255 returns the characters corresponding to the ASCII value, and a string format that accepts any type of return
The code is as follows:
>>> Chr (97)
'A'
>>> Str (97)
'97'
Map and the above functions are used to obtain a list of character values or code values.
The code is as follows:
>>> Print map (ord, (u' \ u54c8 ', u' \ u54c9 '))
[21704,217 05]
>>> Print map (unichr, range (21704,21707 ))
[U' \ u54c8 ', u' \ u54c9', u' \ u54ca ']