What is Python ord ()? What's the use of Ord ()?

Source: Internet
Author: User
Tags chr ord stdin
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.