How does the Format function of a Python string work?

Source: Internet
Author: User

This article and we share the main is the Python string in the Format function related content, a look at it, hope to learn Python to help you.

From python2.6, the string in Python has a powerful tool for the format control of the Str.format () function. The Str.format () function is clearly more in line with our thinking habits and more concise than the previous format control method using%.

Grammar

As a method of a string, it is formatted with {} and: instead of%.

Positioning

By location

inch [1]: ' {0},{1} '. Format (' Kzc ', 18)

out [1]: ' kzc,18 '

inch [2]: ' {},{} '. Format (' Kzc ', 18)

out [2]: ' kzc,18 '

inch [3]: ' {1},{0},{1} '. Format (' Kzc ', 18)

out [3]: ' 18,kzc,18 '

It is very well understood that the value in the {} in the string is used to specify the value in the format to replace him. can accept an unlimited number of parameters, the location can be out of order, can not be used or multiple times.

by keyword parameter

inch [5]: ' {name},{age} '. Format (age=18,name= ' Kzc ')

out [5]: ' kzc,18 '

is to assign values to the elements in the argument list in the form of a key-value pair.

Through object properties

class Person:

def __init__ (self,name,age):

Self.name,self.age = Name,age

def __str__ (self):

return ' This guy was {self.name},is {self.age} old '. Format (self=self)

In [2]: str (person (' kzc ', 18))

OUT[2]: ' This guy is Kzc,is

Specify the properties of the object to display in the parameter list, and pass in the object in format.

by subscript

inch [7]: p=[' kzc ', 18]

inch [8]: ' {0[0]},{0[1]} '. Format (P)

out [8]: ' kzc,18 '

The binding position is passed to the following table.

Format qualifier

Fill and align

Padding is used in conjunction with alignment ^, <, > is centered, left-aligned, right-aligned, followed by the width: the character after the fill, can only be one character, not specified by default is filled with spaces such as

inch []: ' {: >8} '. Format (' 189 ')

out [15]: ' 189 '

inch [+]: ' {: 0>8} '. Format (' 189 ')

out [16]: ' 00000189 '

inch [+]: ' {: a>8} '. Format (' 189 ')

out [+]: ' aaaaa189 '

Accuracy and type

inch [+]: ' {:. 2f} '. Format (321.33345)

out [44]: ' 321.33 '

Accuracy is often used in conjunction with Type F, in this case. 2 represents a precision of length 2, and F represents a float type.

Binary conversion

inch [Si]: ' {: b} '. Format (17)

out [54]: ' 10001 '

inch [+]: ' {:d} '. Format (17)

out [55]: ' 17 '

inch [+]: ' {: o} '. Format (17)

out [56]: ' 21 '

inch []: ' {: x} '. Format (17)

out [57]: ' 11 '

B, D, O, and X are binary, decimal, octal, hexadecimal, followed by a colon

Split character

inch [+]: ' {:,} '. Format (1234567890)

out [47]: ' 1,234,567,890 '

Comma can also be used to make the amount of thousands separator.

Source: Myths's personal blog

How does the Format function of a Python string work?

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.