Python implements the string formatting method summary

  • 2020-05-26 09:35:46
  • OfStack

Python2.6 + adds the str.format function to replace the '%' operator. It is more intuitive and flexible to use than '%'. The following is a detailed description of how to use it.

Here's an example of using '%' :


""
"PI is %f..." % 3.14159 # => 'PI is 3.141590...'
"%d + %d = %d" % (5, 6, 5+6) # => '5 + 6 = 11'
"The usage of %(language)s" % {"language": "python"} # => 'The usage of python'

It's very similar to printf in C, right? Since '%' is an operator, you can only put one parameter on the left and one parameter on the right. Therefore, multiple values on the right need to be included by tuples or dictionaries. Tuple dictionary 1 cannot be used, which lacks flexibility.

The same example is rewritten using the format method:


"PI is {0}...".format(3.14159) # => 'PI is 3.14159...'
"{0} + {1} = {2}".format(5, 6, 5+6) # => '5 + 6 = 11'
"The usage of {language}".format(language = "Python") # => 'The usage of Python'

Is that intuitive? (of course, I also like the first one in C :-))

Formatted string


"{named} consist of intermingled character {0} and {1}".format("data", "markup", \
  named = "Formats trings")
format(10.0, "7.3g") # => '   10'
"My name is {0} :-{{}}".format('Fred') # => 'My name is Fred :-{}'

Note that line 1 '\', if a statement is to wrap, must be escaped with a backslash at the end.

You can't mix tuples and dictionaries like this with '%'. This is actually a named parameter, a feature of Python. You can use *args, **kwargs syntax to expand collections and dictionaries when defining arrays. Note that the named parameter is left behind.

The second statement indicates that the format built-in function is used to format a single value.

The third statement represents the escape of {} because {} is a special character in the formatted string and cannot be displayed directly. The escape mode is multi-nested 1 layer.

Use properties and indexes


"My name is {0.name}".format(open('out.txt', 'w')) # => 'My name is out.txt'

'{0.name}' is equivalent to calling the property open(' out.txt ', 'w').name of the object


"My name is {0[name]}".format(dict(name='Fred')) # => 'My name is Fred'

It is also possible to use indexes.

obj [key] equivalent obj. ____getitem____ (' key ')

Standard specifier (Specifiers)

Programmers who have written C should be aware of the complexity of printf. format also defines a number of standard specifiers that interpret the format of a value and then insert it into the string. Such as:


"My name is {0:8}".format('Fred') # => 'My name is Fred  '

After ':' comes the specifier, which in the above example has only one '8'(minimumwidth), indicating that the insertion value is at least 8 wide. 'Fred' only has 4, so add 4 more Spaces.

The detailed format of the specifier is:

[[fill]align][sign][#][0][minimumwidth][.precision][type] (no more compact than C's printf!)

Note: '[]' indicates that the element is optional. So, all format specifiers are optional! As in the previous example, this is almost never used (just for clarity). In fact, these are useful.

Let's look at each one of us:
1. [fill]align means arrangement. When minimumwidth is set to a value larger than the inserted value, there is white space, as in 'My name is Fred 'in the previous example. By default, white space is placed on the right, which means that the insert value is left aligned by default. If we try {0: > 8}, you will find that the result becomes 'My name is Fred'.
fill represents the characters used to fill the white space. fill is only useful if align specifies it! align can be the following logo:

< Left aligned, default > Align right = put the white space after the align logo, only valid for Numbers. What does that mean? align, as we'll see below, displays the plus and minus signs of Numbers, again only for Numbers. If '=' is specified, the plus or minus sign of the number is displayed before the white space. For example, format(-12, "0=8") # = > '-0000012' notice that the built-in function format is used to format a single value. '0' is the fill element, which fills the white space; '=' is the identifier; '8' means the minimum width is 8, so there is 5 white space. What about align? So, align is essentially the way the plus and minus signs are displayed, and we're going to use the default '-', and we'll talk about that later. Center aligned

2, sign number symbols, only valid for Numbers.

+ shows the plus and minus signs - no plus, no minus. Negative Numbers always take up one more sign than positive Numbers without specifying a minimum width. The default "(1 space) replace the plus sign with 1 blank

3. The prefix of # to display a number is in decimal (0b, 0o, 0x).

4. Fill the blank with '0' for 0.

5. minimumwidth specifies a minimum width, which has been used many times.

precision 'precision' is a hexadecimal number that indicates the number of decimal places to be displayed.

7. Type of type value:

1) integer:

b 2 into the system c character type, converts the number to the character representing unicode d decimal o 8 hexadecimal x 106 in base, showing lowercase letters X 106 in base, showing uppercase letters n behaves the same as d, using a local number representation "(empty, no Spaces) is the same as d

(2) floating point number

e scientific notation, e lowercase E scientific notation, capital E f is shown as a fixed point number, with a default of 6 decimal places F with f g automatically selects whether to use scientific notation G with g n and g are represented locally % is expressed as a percentage "(empty) same as g

Each object can override its own formatting specifier, as shown by the datatime class:


"Today is: {0:%a %b %d %H:%M:%S %Y}".format(datetime.now())

Transformation in advance

':' is followed by the format specifier, which can be preceded by a pre-converted identifier

! r calls the _repr_ method of the object to convert to a standard string ! s calls the _str_ method of the object to convert to a string

Override the _format_ method

When we format a string, we first format each value and then insert it into the string. The formatted value calls the format built-in method. format simply calls the _format_ method of the value.


def format(value, format_spec):
  return value.__format__(format_spec)

The _format method is implemented in the object class, only to convert itself to a string using str(), and then pass the string into the built-in format method, which is essentially a call to the format_ method converted to a string.


class object:
  def __format__(self, format_spec):
    return format(str(self), format_spec)

int/float/str itself implements the _format_ method, whose respective specifiers were described earlier.

conclusion

There is also a bit of custom Formatter, which you don't usually use. Leave that for the next string module source code interpretation. Suggest interested friends to see Python standard library source code, very valuable to learn.


Related articles: