Python Chr () ord () UNICHR ()

Source: Internet
Author: User
Tags chr ord stdin

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.

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

>>> Chr (69)'E'>>> Ord ("A")65>>> UNICHR (500) U'\u01f4'>>>PrintUNICHR (500) Traceback (most recent): File"<stdin>", Line 1,inch<module>Unicodeencodeerror:'GBK'Codec can'T encode character u'\u01f4'In position 0:illegal multibyte sequence#can be encode into UTF8 output when all encoding formats fail to pass>>>PrintUNICHR (+) encode ("Utf-8") Punished>>> A=u'Punished'>>>Ord (a)35892>>> a='Punished'>>>Ord (a) Traceback (most recent): File"<stdin>", Line 1,inch<module>Typeerror:ord () expected a character, but string of length2found>>> Chr (35892) Traceback (most recent): File"<stdin>", Line 1,inch<module>VALUEERROR:CHR () arg not inchRange (256)>>> UNICHR (35892) U'\u8c34'>>>PrintUNICHR (35892). Encode ("Utf-8") Zhang>>>PrintUNICHR (35892). Encode ("GBK") Punished

Python Chr () ord () UNICHR ()

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