String formatting.

Source: Internet
Author: User

String formatting

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

When formatting a string, Python uses a string as a template. There are formatting characters in the template that reserve locations for real values and describe the format in which real values should be rendered. Python uses a tuple to pass multiple values to the template, each of which corresponds to a format character.

Percent-Semicolon mode:

The format can be further controlled in the following ways

%[(name)][flags][width]. [Precision]typecode (name) for name flags can have +,-, ' or 0. + Indicates right alignment. -Indicates left alignment. ' is a space that fills a space on the left side of a positive number to align with a negative number. 0 means using 0 padding. Width indicates the display width precision indicates the precision after the decimal point

[]: Indicates an optional

fill: Optional, white space filled characters

align: optional, on its way (should be used 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 digits

    • +, positive home Plus, minus plus negative;
    • -The plus sign does not change, minus is negative;
    • Spaces, plus spaces, minus signs and negative;

#: Optional, for binary, octal, hex, if Add #, will show 0b/0o/ox, otherwise not display

,: Optional, add separators for numbers, such as: 1,000,000

width: Optional, the format of the occupied position

. Precision: Optional, decimal retention accuracy

Type: Optional, formatted type

    • passed arguments for string type
        • s, format string type data
        • blank, unspecified Type, the default is None, with the S
    • passed in an integer type parameter
        • B, convert the 10 binary integers automatically to a 2 binary representation and then format
        • C, automatically converts a 10-based integer to its corresponding Unicode character
        • d, decimal integer
        • O, the 10 binary integers are automatically converted to 8 binary representations and then formatted;
        • x, automatically converts a 10-based integer to a 16-binary representation and then formats (lowercase x)
        • X, automatically converts a 10-based integer to a 16-binary representation and then formats (uppercase X)
    • incoming float or decimal type   parameter
        • E, converted to scientific notation (lowercase e), then formatted;
        • E, converted to scientific notation (capital E) after formatting;
        • 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 (the default is 6 digits after the decimal)
s = ' I am%s,age%d '% (' kai ', +) print (s) execution result: I am kai,age 19s = ' I am% (N1) S,age% (N2) s '% {' N1 ': ' Kai ', ' N2 ': ' + '}print (s ) Execution result: I am kai,age 100s = ' percent%.2f '% 1.2222print (s) execution result: percent 1.22s = ' I am% (ZZ). 2f '%{' zz ': 3.555555}print (s) execution result: I am 3.56s = ' I am% (kai). 2f%%{' kai ': 2.55555}print (s) execution result: I am 2.56

String formatting.

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.