Comparison between CHR, UNICHR, and Ord character functions in Python

Source: Internet
Author: User
Tags ord
CHR, UNICHR, Ord can be used as character-type conversions in Python, and here we talk about the comparison between CHR, UNICHR, and Ord character functions in Python, and the friends you need can refer to

    • ORD is the abbreviation for Unicode ordinal, which is the number

    • CHR is the abbreviation of character, which is the character

    • Ord and CHR are reciprocal conversions.

    • However, because CHR is limited to ASCII, the length is only 256, so more a unichr.

>>c = U ' kang ' >>cu ' \u5eb7 ' >>ord (c) 24747>>chr (24247) VALUEERROR:CHR () arg not in range > >UNICHR (24247) u ' \u5eb7 '

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 ' 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.