"Python CookBook2" chapter I text-converting between one character && character and character value at a time

Source: Internet
Author: User
Tags ord

Text-Summary:

    1. What is Text
      The string type in Python is an immutable type.
      Text, a matrix of characters, every single text can be indented and organized quickly.

    2. Basic Text manipulation
      ① parse the data and put the data into the structure inside the program;
      ② the data in some way into another similar form, the data itself has changed;
      ③ to generate new data

process one character at a time:
  • Task
    The string is processed with one character at a time.

  • Solution Solutions
    You can create a list of subkeys that are characters of a string.
    The code is as follows:
    >>> thelist = List ('thestring')>>>thelist['T','h','e','s','T','R','I','N','g']>>> forCharinchthelist:PrintChar thestring>>> Map (LambdaX:x*3, Thelist) ['TTT','HHH','Eee','SSS','TTT','RRR','III','nnn','GGG']

Conversion between character and character values
  • Task
    Convert a character to the corresponding Ascⅱ (ISO) or Unicode code, or to the contrary.

  • Solution Solutions
    Built-in Functions Ord and CHR:
    >>> Ord ('a')97>>> chr'a  '


    If you want to convert a string into a list of values that contain individual characters, you might want to use the built-in map and ORD functions at the same time:

    Print map (ord,'python') [112, 121, 116, 104, 111, 110]


    If you want to create a string from a list that contains a character value, you can use the '. Join, map, and CHR

    Print ". Join (Map (Chr,range (97,100))) ABC

Experience:

1. Programming skills more reference to others, will become their own

2. The more basic the more you should remember and consolidate

3. It is said that the system business 80% is the processing of characters, so carefully study it

4. Do not accumulate kuibu to thousands of miles

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.