Use of format function in python
The basic syntax is to replace the preceding% with {} and:.
For example, 1 outputs a two-digit number: print ("{:.2f}"). Format (3. 1456879)), rounded.
2. The index can be filled with braces to ensure the sequential position of parameters in subsequent formats, such as:
Print ("{2}{0}{ 1}". Format ("Yes", "You", "How"))
The output is Howareyou. Note that the index should start from 0.
Moreover, it should be noted that if the index is not filled in, it will be filled in the order of parameters in format by default, and the number of braces should be equal to the format parameters. If the indexes are filled, the number of indexes needs to be no greater than the number of format indexes.
3. If it is a double-layer brace structure, it is considered that the second-layer brace is an ordinary string and will not be formatted. For example:
Print("{} corresponds to {{ordinary string}} ". Format ("string")).
The output is: the corresponding position of the string is {ordinary string}, pay attention to the bold part, and the second layer of braces will be used as ordinary string and will not be formatted.