The conversion of letters and ASCII codes in Python

Source: Internet
Author: User
Tags iterable ord

In the Python programming, encountered the need to convert letters to ASCII code, originally thought with an int () can directly convert the string into a shape, but int () with a default parameter, base=10, here is the decimal, if the letter appears, it will be an error, The representation range that is considered to be outside the binary.

The use of several functions is clarified by looking at the Web and Python help documents, which are recorded as follows:

Ord (c): The parameter is a string of length 1, the abbreviation character. When the parameter is a uniform object (Unicode object), returns a uniform encoding that represents the character, and returns the value of the byte when the argument is a 8-bit string. For example, Ord (' a ') returns the Shaping value 97,ord (U ' \u2020 ') returns 8224.

Chr (i): Returns a character with the ASCII code equal to the Shaping value in the parameter. For example CHR (97) Returns the character ' a ', which is the inverse of the Ord () method. The parameter must be an integer value of 0-255, or a valueerror error will be thrown.

When applying a function, it is observed that a python function is more efficient, map (function,iterable,...) Applies a function to each item iterable, and returns a list of results. map () is a Python built-in high-order function that receives a function f and a list, and then, by putting the function f on each element of the list in turn, gets a new list and returns.

Note: the map () function does not change the original list, but instead returns a new list.

With the map () function, you can convert a list to another list, just pass in the conversion function.

Because the list contains elements that can be of any type, map () can not only handle lists that contain only numeric values, but in fact it can handle lists of any type, as long as the incoming function f can handle the data type.

The conversion of letters and ASCII codes 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.