Python Master's path The string formatting of the "six" Python Foundation

Source: Internet
Author: User

There are two ways to format a Python string: a percent-semicolon, format-mode

The percent of the semicolon is relatively old, and format is a more advanced way to replace the old way, the two coexist. [PEP-3101]

This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing '% ' s Tring formatting operator.

1, percent of the way

%[(name)][flags][width]. [Precision]typecode

  • (name) optional, used to select the specified key
  • Flags optional, selectable values are: width selectable, occupied
    • + Right alignment, positive home Plus, negative number before plus minus;
    • -left-justified; no sign before positive number, plus minus sign before negative number;
    • Spaces are right-justified, plus a positive number before a negative number plus a minus sign;
    • 0 Right alignment, no sign before positive number, minus sign before negative, and 0 padding in blank
  • . Precision optional, number of digits retained after the decimal point
  • typecode     required
    • S, Gets the return value of the __str__ method of the passed-in object and formats it to the specified location
    • R, gets the return value of the __repr__ method of the passed-in object, and formats it to the specified location
    • C, Integer: Converts a number to its Unicode corresponding value, the 10 binary range is  0 <= i <= 1114111 (py27 only supports 0-255); character: Adds a character to the specified location
    • O, converts an integer to eight   and formats it to a specified location
    • x, converts an integer to a hexadecimal representation, and formats it to a specified location
    • D, converting integers, floating-point numbers to decimal representations, and formats it to the specified location
    • E, converts an integer, floating-point number to scientific notation, and formats it to a specified position (lowercase e)
    • E, converting integers, floating-point numbers to scientific notation, and formats it to the specified position (capital E)
    • F, converts an integer, floating-point number to a floating-point number representation, and formats it to a specified location (the default is 6 digits after the decimal point)
    • F, ibid.
    • g, auto-adjust converts integers, floating-point numbers to floating-point or scientific notation (more than 6 digits in scientific notation), and formats it to a specified location (if scientific count is E;)
    • G, auto-adjust converts integers, floating-point numbers to floating-point or scientific notation (more than 6 digits in scientific notation), and formats it to a specified location (if scientific count is E;)
    • %, when there is a format flag in the string, a percent sign is required to represent a semicolon

Note: There is no way to automatically convert an integer to a binary representation in Python with a percent-semicolon format

Common formatting:

1TPL ="I am%s"%"Poe"2  3TPL ="I am%s age%d"% ("Poe", 18)4  5TPL ="I am% (name) s age percent (age) D"% {"name":"Poe"," Age": 18}6  7TPL ="percent%.2f"% 99.976238  9TPL ="I am% (PP). 2f"% {"pp": 123.425556, }Ten   OneTPL ="I am%.2f percent"% {"pp": 123.425556,}
2. Format Mode

[[Fill]align] [Sign] [#] [0] [Width] [,] [. Precision] [Type]

  • Fill "optional" white space filled with characters
  • Align "optional" alignment (required with width)
      • <, content left justified
      • Content right-aligned (default)
      • =, the content is right-aligned, the symbol is placed to the left of the padding character, and only valid for numeric types. Even: symbol + filler + number
      • ^, content centered
  • Sign "optional" with unsigned number # "optional" for binary, octal, hexadecimal, if you add #, it will display 0b/0o/0x, otherwise not display
    • +, positive home Plus, minus plus negative;
    • -The plus sign does not change, minus is negative;
    • Spaces, plus spaces, minus signs and negative;
  • , "optional" adds a delimiter to the number, such as: 1,000,000
  • width "Optional" format the size of the placeholder
  • . Precision "optional" decimal digits reserved Precision
  • type         "optional" format type
    • parameter passed into string type
      • S, formatting string type data
      • blank, no type specified, default is None, with S
    • incoming argument to integer type
      • B, converting 10-integer integers to 2-binary notation and then formatting
      • C, converting 10-integer integers to their The expected Unicode character
      • d, decimal integer
      • o, automatically converts a 10-decimal integer to a 8-binary representation and then formats it;
      • x, and 10 binary integers from Move to a 16 binary representation and then format (lowercase x)
      • x to automatically convert 10 binary integers to 16 binary representations and then format (uppercase X)
    • incoming floating-point or decimal type &N BSP; " The parameter
      • E, converted to scientific notation (lowercase e) is represented, and then formatted;
      • E, converted to scientific notation (capital E), and then formatted;
      • F, converted to floating point (6 bits left after the default decimal point), and then formatted;
      • F, converted to floating point (6 bits left after the default decimal point), and then formatted;
      • g, automatically toggle between E and F
      • G, automatically toggle between E and F
      • %, display percent (6 digits after the decimal point by default)

Common formatting:

1TPL ="I am {}, age {}, {}". Format ("Seven", 18,'Alex')2   3TPL ="I am {}, age {}, {}". Format (*["Seven", 18,'Alex'])4   5TPL ="I am {0}, age {1}, really {0}". Format ("Seven", 18)6   7TPL ="I am {0}, age {1}, really {0}". Format (*["Seven", 18])8   9TPL ="I am {name}, age {age}, really {name}". Format (name="Seven", age=18)Ten    OneTPL ="I am {name}, age {age}, really {name}". Format (**{"name":"Seven"," Age": 18}) A    -TPL ="I am {0[0]}, age {0[1]}, really {0[2]}". format ([1, 2, 3], [11, 22, 33]) -    theTPL ="I am {: s}, age {:d}, Money {: F}". Format ("Seven", 18, 88888.1) -    -TPL ="I am {: s}, age {:d}". Format (*["Seven", 18]) -    +TPL ="I am {name:s}, age {age:d}". Format (name="Seven", age=18) -    +TPL ="I am {name:s}, age {age:d}". Format (**{"name":"Seven"," Age": 18}) A   atTPL ="numbers: {: b},{:o},{:d},{:x},{:x}, {:%}". Format (15, 15, 15, 15, 15, 15.87623, 2) -   -TPL ="numbers: {: b},{:o},{:d},{:x},{:x}, {:%}". Format (15, 15, 15, 15, 15, 15.87623, 2) -   -TPL ="numbers: {0:b},{0:o},{0:d},{0:x},{0:x}, {0:%}". Format (15) -   inTPL ="numbers: {num:b},{num:o},{num:d},{num:x},{num:x}, {num:%}". Format (NUM=15)

More formatting operations: https://docs.python.org/3/library/string.html

Python Master's path The string formatting of the "six" Python Foundation

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.