Tour Python Seven: string formatting: Percent manner and format mode

Python string formatting in two ways: the percent mode and format mode

Percent way is relatively old, and the format is more advanced way, and may replace the percent sign in the form, currently both coexist.

1, the percent sign way

%[(name)][flags][width].[precision]typecode
  • (Name) Alternatively, for selecting a specified key
  • flags optional, alternative values ​​are:
    • +       Right aligned; added just before the positive, negative numbers preceded by a minus sign;
    • - Left; unsigned positive front, a minus sign before the negative;
    • Right-aligned spaces; spaces, a minus sign before the negative before positive number;
    • Align Right 0; unsigned before positive and negative numbers preceded by a minus sign; the space filled with 0
  • width optional, possession of width
  • .precision optional decimal places reserved
  • typecode Required
    • s, __str__ method of obtaining the return value of the incoming object and is formatted to the specified position
    • r, __repr__ method of obtaining the return value of the incoming object and is formatted to the specified position
    • C, integers: a number into its corresponding unicode values, decimal range 0 <= i <= 1114111 (py27 only supports 0-255); character: adding a character to the specified position
    • O, the octal notation is converted into an integer, and formats it into the designated location
    • x, an integer converted into hexadecimal notation, and formats it into the designated location
    • d, convert integer, floating point decimal representation and formats it to a designated position
    • E, integer, floating-point numbers into scientific notation, and formats it into the designated location (lowercase e)
    • E, converting the integer, floating-point number in scientific notation, and formats it to the specified position (uppercase E)
    • F, converting integers, floating point numbers to floating point representation and formats it to the specified position (after the default 6 decimal places)
    • F, ibid.
    • g, automatic adjustment convert integer, float to float or scientific notation (more figures in scientific notation), and formats it to a specified location (if it is a scientific notation E;)
    • G, automatic adjustment convert integer, float to float or scientific notation (more figures in scientific notation), and formats it to a specified location (if it is a scientific notation E;)
    • %, When the flag is present formatted string, a percent sign indicates required by %%

Note: Python the percent format is automatically converted into a binary representation of an integer as not exist

Common Formatting:

1 tpl = "i am %s" % "alex"
 
2 tpl = "i am %s age %d" % ("alex", 18)
 
3 tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
 
4 tpl = "percent %.2f" % 99.97623
 
5 tpl = "i am %(pp).2f" % {"pp": 123.425556, }
 
6 tpl = "i am %.2f %%" % {"pp": 123.425556, }

2, Format way

[[fill]align][sign][#][0][width][,][.precision][type]
  • [Optional] fill the space filled with character
  • [optional] align alignment (in conjunction with the use of width)
    • <, Left-aligned content
    • > Contents in the right alignment (default)
    • = Contents in the right alignment, the symbol on the left pad characters, and only valid digital type. Even: digital sign + filler +
    • ^, Centered content
  • sign [optional] whether the symbol numbers
    • +, Positive plus plus plus minus minus sign;
    •  - a positive sign change, plus minus negative sign;
    • Plus negative space, plus space, negative sign;
  • # [Optional] For binary, octal, hexadecimal, if coupled with # displayed 0b / 0o / 0x, or do not show
  • , [Optional] is added to a digital separators, such as: 1,000,000
  • [optional] format bit width occupied by the width
  • .precision [optional] decimal place precision reserved
  • type [optional] format type
    • Incoming "string type" parameter
      • s, string type data format
      • Blank, unspecified type, the default is None, with s
    • Incoming "integer type" parameter
      • b, will be automatically converted to decimal integer of 2 hexadecimal format then
      • c, will automatically convert a decimal integer to its corresponding unicode character
      • d, decimal integer
      • o, it will be automatically converted to decimal integer octal and format;
      • x, the decimal integer automatically converted into a hexadecimal format and (lower case x)
      • X, will be automatically converted to decimal integer in hexadecimal format and then (upper case X)
    • Passed "or decimal floating-point type" parameter
      • E, is converted to scientific notation (lowercase e) shows, then format;
      • E, is converted to scientific notation (uppercase E), and format;
      • F, is converted to floating point (the default after the decimal point 6) shows, then format;
      • F., Is converted to floating point (the default after the decimal point 6) shows, then format;
      • g, e and f are automatically switched
      • G, automatically switches E and F
      • %, Shows the percentage (default display 6 decimal place)

 Common Formatting:

1 tpl = "i am {}, age {}, {}".format("seven"18'alex')

2 tpl = "i am {}, age {}, {}".format(*["seven"18'alex'])

3 tpl = "i am {0}, age {1}, really {0}".format("seven"18)

4 tpl = "i am {0}, age {1}, really {0}".format(*["seven"18])

5 tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)

6 tpl = "i am {name}, age {age}, really {name}".format(**{"name""seven""age"18})

7 tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([123], [112233])

8 tpl = "i am {:s}, age {:d}, money {:f}".format("seven"1888888.1)

9 tpl = "i am {:s}, age {:d}".format(*["seven"18])

10 tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)

11 tpl = "i am {name:s}, age {age:d}".format(**{"name""seven""age"18})

12 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(151515151515.876232)

13 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(151515151515.876232)

14 tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)

15 tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)

More formatting operation, see: https: //docs.python.org/3/library/string.html

 

Guess you like

Origin blog.csdn.net/xymalos/article/details/91509529