Python development [Article 1] string formatting Based on Python, Article 1 python

Source: Internet
Author: User

Python development [Article 1] string formatting Based on Python, Article 1 python

String formatting

There are two ways to format Python strings: percent and format.

The percent sign method is relatively old, while the format method is relatively advanced. It attempts to replace the old one. Currently, both methods coexist. [EP-3101]

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

 

1. percent sign Method

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

 

1. Sequential Parameters
2. specify the name of the input parameter
3. Retain the digits after the decimal point
4. If a placeholder is displayed, write %. When printing %.

  • (Name) (optional) used to select the specified key
  • (Optional) flags. Optional values include:
  • + Right alignment; add positive values before positive values, and add negative numbers before negative values;
  • -Left alignment; unsigned before a positive number; added a negative number before a negative number;
  • Right alignment of spaces; add spaces before positive numbers, and add negative signs before negative numbers;
  • 0: right alignment; positive: Unsigned; negative: added before negative; blank space filled with 0
  • Width (optional) occupies the width.
  • . Precision (optional) number of digits retained after decimal point
  • Typecode required
  • S, get the return value of the _ str _ method of the input object, and format it to the specified location
  • R, get the return value of the _ repr _ method of the input object, and format it to the specified location
  • C, integer: converts a number to its unicode value. The value range of decimal is 0 <= I <= 1114111 (py27 only supports 0-255). character: add characters to a specified position
  • O. Convert the integer into an octal representation and format it to the specified position.
  • X. Convert the integer to a hexadecimal representation and format it to the specified position.
  • D. Convert integers and floating-point numbers into a decimal representation and format them to the specified position.
  • E. Convert integers and floating-point numbers into scientific notation and format them to the specified position (lower case e)
  • E. Convert integers and floating-point numbers into scientific notation and format them to the specified position (uppercase E)
  • F. Convert integers and floating-point numbers to floating-point numbers and format them to the specified position (the default value is 6 digits after the decimal point)
  • F, same as above
  • G. It is automatically adjusted to convert integers and floating-point numbers into floating-point or scientific notation (more than 6 digits are counted in scientific Notation ), format it to the specified position (e for scientific counting ;)
  • G. It is automatically adjusted to convert integers and floating-point numbers into floating-point or scientific notation (more than 6 digits are counted in scientific Notation ), format it to the specified position (E for scientific counting ;)
  • %. When a formatted sign exists in the string, % must be used to indicate a percent sign.

Note: In Python, the hundred-semicolon format does not automatically convert integers into binary representation.

 

Common formatting:

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

  

 2. Format

The format can be centered, % Percent, binary, and number plus commas, and filled. % Cannot.

[[fill]align][sign][#][0][width][,][.precision][type]
  • Fill [Optional] characters filled in the blank space
  • Align [Optional] align (which must be used with width)
  • <, Left-aligned content
  • >, Right-aligned content (default)
  • =, The content is right aligned, the symbol is placed on the left side of the fill character, and only valid for the number type. Even if: Symbol + padding + number
  • ^, Center content
  • Sign [Optional] with or without a signed number # [Optional] For binary, octal, and hexadecimal systems, if # is added, 0b/0o/0x is displayed; otherwise, it is not displayed.
  • +, Positive and negative;
  • -The positive value remains unchanged, and the negative value is added to the negative value;
  • Space, positive space, negative plus negative;
  • , [Optional] Add a separator for a number, for example, 1,000,000
  • Width [Optional] width occupied by the formatting bit
  • . Precision [Optional] decimal point retention precision
  • Type [Optional] format type
  • Input "string type" parameter
  • S: Format String data.
  • Blank. If no type is specified, the default value is None, which is the same as s.
  • Input "Integer type" parameter
  • B. Convert the decimal integer into a binary representation and format it.
  • C. convert a 10-in-10 integer to its corresponding unicode Character
  • D, decimal integer
  • O. Convert the decimal integer into an octal integer and format it;
  • X: Convert the decimal integer into a hexadecimal representation and format it (lowercase x)
  • X: Convert the decimal integer into a hexadecimal representation and format it (uppercase X)
  • Input parameters of the float or decimal type
  • E. Convert to scientific notation (lower case e) and format it;
  • E. Convert to scientific notation (uppercase E) and format it;
  • F. convert it to the floating point type (6 digits after the decimal point by default) and format it;
  • F. convert it to the floating point type (6 digits after the decimal point by default) and format it;
  • G, automatically switch between e and f
  • G, automatically switch between E and F
  • %, Display percentage (6 digits after decimal point by default)

Common formatting:

tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')  tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])  tpl = "i am {0}, age {1}, really {0}".format("seven", 18)  tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])  tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)  tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})  tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])  tpl = "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}) tpl = "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) tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)

Important:

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

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.