The ORD,CHR function in Python

Source: Internet
Author: User
Tags ord

Chr (), UNICHR () and Ord ()

The CHR () function returns a corresponding character by using an integer (that is, 0~255) within range (256) as a parameter. UNICHR () is just like it, except that the Unicode character is returned, and the parameter range of the UNICHR () added from Python 2.0 depends on how your Python is compiled. If it is configured to USC2 Unicode, then it is allowed to range (65536) or 0X0000-0XFFFF, if configured as UCS4, then this value should be range (1114112) or 0x000000-0x110000. If the supplied parameter is not within the allowable range, a ValueError exception is reported.

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.

>>> Chr (65)

A

>>> Ord (' a ')

97

>>> UNICHR (12345)

U ' \u3039 '

>>> chr (12345)

Traceback (most recent):

File "<stdin>", line 1, in?

Chr (12345)

VALUEERROR:CHR () arg not in range (256)

>>> ord (U ' \ufffff ')

Traceback (most recent):

File "<stdin>", line 1, in?

Ord (U ' \ufffff ')

Typeerror:ord () expected a character, but string of length 2 found

>>> ord (U ' \u2345 ')

9029

The ORD,CHR function in Python

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.