This chapter describes the
ord in Python ()The meaning and function of functions, in general,
ord () functionMainly used to return the corresponding characters of the ASCII code, CHR () is mainly used to denote the ASCII code corresponding to the character of his input when the number, can be in decimal or hexadecimal. That is, the Ord () function is the Chr () function (for 8-bit ASCII strings) or the UNICHR () function (for Unicode objects), which returns the corresponding ASCII value, or Unicode value, as a parameter with a character (a string of length 1). If the given Unicode character exceeds your Python definition range, a TypeError exception is thrown.
1 >>> Ord ("a") 2 973 >>> Chr (4 ' a ')
For example, to generate an alphabet list, we can do this:
>>> [Chr (i) for I in Range (97,123)] [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' p ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' Z ']
# user Input character c = input ("Please enter a character:") # The user enters the ASCII code and converts the input number to integer a = Int (input ("Please enter an ASCII code:")) print (c + "ASCII code is", Ord (c)) Prin T (A, "corresponding character is", Chr (a))
1 Enter a character: A2 Please enter an ASCII code: 1013 A's ASCII code is 974 101 corresponding character is E
or this:
>>> chr ' 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 (at >>> 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