Conversion of Python characters and character values (ASCII or Unicode code value)
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 ']