Talking about the comparison between chr, unichr, and ord character functions in Python, unichrord
- Ord is the abbreviation of unicode ordinal, that is, number.
- Chr is the abbreviation of character, that is, the character
- Ord and chr convert each other.
- However, because chr is limited to ascii and has a length of only 256, an unichr is added.
> C = u'kang'> cu '\ u5eb7'> ord (c) 24747> chr (24247) ValueError: chr () arg not in range (256)> unichr (24247) U' \ u5eb7'
The chr () function uses a range (0 to 256 ~ 255) an integer is used as a parameter and a corresponding character is returned. Unichr () is the same as it, but only returns Unicode characters. The parameter range of unichr () added from Python 2.0 depends on how your Python is compiled. If Unicode is configured as USC2, the allowed range is range (65536) or 0x0000-0xFFFF. If it is configured as UCS4, the value should be range (1114112) or 0x000000-0x110000. If the provided parameter is not within the permitted range, a ValueError exception is reported.
The ord () function is a paired function of the chr () function (for an 8-bit ASCII string) or unichr () function (for a Unicode object, it takes a single character (a string with a length of 1) as a parameter and returns the corresponding ASCII value or Unicode value. If the Unicode character is out of your Python defined range, A TypeError exception is thrown.
>>> chr(65)'A'>>> ord('a')97>>> unichr(12345)u'\u3039'>>> chr(12345)Traceback (most recent call last): File "<stdin>", line 1, in ? chr(12345)ValueError: chr() arg not in range(256)>>> ord(u'\ufffff')Traceback (most recent call last): File "<stdin>", line 1, in ? ord(u'\ufffff')TypeError: ord() expected a character, but string of length 2 found>>> ord(u'\u2345')9029