Python's powerful string formatting function-format

Source: Internet
Author: User
Since python26, strformat () is a new function for formatting strings, which is powerful. So What advantages does it have compared with the previous % formatted string? Since python2.6, a new string formatting function str. format () has been added, which is powerful. So What advantages does it have compared with the previous % formatted string? Let's unveil it.

Syntax

It uses {} and: to replace %

Location formatting

>>> '{}.{}'.format('pythontab', 'com')'pythontab.com'>>> '{}.{}.{}'.format('www', 'pythontab', 'com')'www.pythontab.com'>>> '{1}.{2}'.format('www', 'pythontab', 'com')'pythontab.com'>>> '{1}.{2} | {0}.{1}.{2}'.format('www', 'pythontab', 'com')'pythontab.com | www.pythontab.com'

The format function of a string can accept unlimited parameters. parameter positions can be in different order. parameters can be used or not multiple times, which is flexible.

Note: the value of python2.6 cannot be null {}. you can use python2.7 or a later version.

Keyword parameters

>>> '{domain}, {year}'.format(domain='www.pythontab.com', year=2016)'www.pythontab.com, 2016'>>> '{domain} ### {year}'.format(domain='www.pythontab.com', year=2016)'www.pythontab.com ### 2016'>>> '{domain} ### {year}'.format(year=2016,domain='www.pythontab.com')'www.pythontab.com ### 2016'

Object attributes

>>> class website:         def __init__(self,name,type):             self.name,self.type = name,type         def __str__(self):           return 'Website name: {self.name}, Website type: {self.type} '.format(self=self) >>> print str(website('pythontab.com', 'python'))Website name: pythontab.com, Website type: python>>> print website('pythontab.com', 'python')Website name: pythontab.com, Website type: python

Subscript

>>> '{0[1]}.{0[0]}.{1}'.format(['pyhtontab', 'www'], 'com')'www.pyhtontab.com'

With these convenient "ing" methods, we have the powerful tool of laziness. The basic knowledge of python tells us that list and tuple can be converted into common parameters to the function through "break", while dict can break into keyword parameters to the function (pass and *). Therefore, you can easily upload a list, tuple, or dict to the format function, which is very flexible.

Format qualifier

It has a variety of "format delimiters" (the syntax is {} with a: number), such:

Fill and align

Fill is often used together with alignment

^, <,> Are center, left, right, and back with width

: Enter only one character after the number. If this parameter is not specified, spaces are used by default.

Code example:

>>> '{:>10}'.format(2016)'      2016'>>> '{:#>10}'.format(2016)'######2016'>>> '{:0>10}'.format(2016)'0000002016'

Digital Precision and Type f

Precision is often used with Type f.

>>> '{:.2f}'.format(2016.0721)'2016.07'

Where. 2 indicates the precision of length 2, and f indicates the float type.

Other types

B, d, o, and x are binary, decimal, octal, and hexadecimal respectively.

>>> '{:b}'.format(2016)'11111100000'>>> '{:d}'.format(2016)'2016'>>> '{:o}'.format(2016)'3740'>>> '{:x}'.format(2016)'7e0'>>>

The number can also be used as the thousands separator for the amount.

>>> '{:,}'.format(20160721)'20,160,721'

The format function is so powerful that you can try it out.

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.