[Python] Formatting method format

Source: Internet
Author: User

All the rules that are included are described first

Curly brace Declaration {}: parameter reference declaration before rendering, curly brackets can be used to denote the ordinal number of the reference parameter, or the variable name is directly referenced. The variable name introduced from the format parameter colon: The number of character bits declares the declaration of a blank auto-fill character declaration of a variable-type declaration: string s, number D, float f-align direction symbol < ^ > property accessor in parentheses? Use an exclamation point! followed by A, R, S, the declaration is the mode of use, acsii mode, reference __repr__ or __str__ Add Class Magic function __format__ (self, format), can be based on the format of the string before format to customize the different display, such as: ' {: xxxx  the XXXX will be passed into the __FORMAT__ function as a parameter at this time.

Next look at the specific use

>>> ' {: >18,.2f} '. Format (70305084.0) #: Colon + blank padding + right-aligned + fixed-width 18 + floating-point precision. F ' 70,305,084.00 ' in floating-point number declaration
>>> data = [4, 8, +, 23, 42]>>> ' {d[4]} {d[5]} '. Format (d=data) ' 42 '
>>> class Plant (object): ...     Type = ' Tree '     ... kinds = [{' name ': ' Oak '}, {' name ': ' Maple '}] ... >>> ' {P.type}: {P.kinds[0][name]} '. Format (p=plant ()) ' Tree:oak ‘
>>> ' {:. 5} '. Format (' xylophone ') ' xylop '
>>> ' {: 4d} '. Format (  42 ')
>>> ' {:. {}} '. Format (' xylophone ', 7) ' Xylopho '
>>> ' {: 06.2f} '. Format (3.141592653589793) #填补符 + fixed width + floating point precision + variable type ' 003.14 '
>>> from datetime import datetime>>> ' {:%y-%m-%d%h:%m} '. Format (DateTime (2001, 2, 3, 4, 5)) ' 2001-02-03 04:05 '

[Python] Formatting method format

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.