In many programming languages, there is the ability to format strings, such as formatted input and output in C and Fortran languages. Python has a built-in operation to format the string.
Template
When formatting a string, Python uses a string as a template. There are formatting characters in the template that reserve locations for real values and describe the format in which real values should be rendered. Python uses a tuple to pass multiple values to the template, each of which corresponds to a format character.
For example, the following:
Print ("I ' m%s.") I ' m%d year old "% (' Vamei ', 99))
In the example above,
"I ' m%s. I ' m%d year old "for our template. %s is the first format character, which represents a string. %d is the second format character, which represents an integer. (' Vamei ', 99) Two elements ' Vamei ' and 99 are substituted for the true values of%s and%d.
Between the template and the tuple, there is a% number separated, which represents the format operation.
The entire "I ' m%s. I ' m%d year old "% (' Vamei ', 99) actually forms a string expression. We can assign a value to a variable as if it were a normal string. Like what:
A = "I ' m%s." I ' m%d year old "% (' Vamei ') print (a)
We can also use dictionaries to pass real values. As follows:
Print ("I ' m% (name) s. I ' m% (age) d year old "% {' name ': ' Vamei ', ' Age ': 99})
As you can see, we have named two of the format characters. The name is used () enclosed. A key for each named corresponding dictionary.
Format character
The format character reserves the location for the real value and controls the format of the display. A format character can contain a type code that controls the type of display, as follows:
%s string (shown with STR ())
%r string (shown with repr ())
%c single character
%b binary integers
%d decimal integers
%i Decimal Integer
%o Eight-binary integers
%x hexadecimal integer
%e index (base written as E)
%E index (base written as E)
%f floating Point
%F floating-point number, same as above
%g index (e) or floating point number (depending on display length)
%G Index (E) or floating point number (depending on display length)
Percent% character "%"
The format can be further controlled in the following ways:
%[(name)][flags][width]. [Precision]typecode
(name) is named
Flags can have +,-, ' or 0. + Indicates right alignment. -Indicates left alignment. ' is a space that fills a space on the left side of a positive number to align with a negative number. 0 means using 0 padding.
Width indicates display widths
Precision indicates the precision after the decimal point
Like what:
Print ("%+10x") print ("%04d"% 5) print ("%6.3f"% 2.3)
The width above, precision is two integers. We can use * to dynamically substitute these two quantities. Like what:
Print ("%.*f"% (4, 1.2))
Python actually replaces * with 4来. So the actual template is "%.4f".
The built-in% operator in Python can be used to format string operations and control the rendering format of strings. There are other ways to format strings in Python, but the use of the% operator is most convenient.
In addition, Python has a more powerful string-handling function Str.format ()
Grammar
It replaces% with {} and:.
Map sample
By location
In [1]: ' {0},{1} '. Format (' Kzc ', +) out[1]: ' kzc,18 ' in [2]: ' {},{} '. Format (' Kzc ', +) out[2]: ' kzc,18 ' in [3]: ' {1},{0},{1} '. Format (' Kzc ', out[3]: ' 18,kzc,18 '
The Format function of the string can accept an unlimited number of parameters, the position can be out of order, can not be used or multiple times, but 2.6 can not be empty {},2.7.
by keyword parameter
In [5]: ' {name},{age} '. Format (age=18,name= ' KZC ') out[5]: ' kzc,18 '
Through object properties
Class Person: def __init__ (self,name,age): self.name,self.age = Name,age def __str__ (self):
In [2]: str ("person (' kzc ')") out[2]: ' This guy is Kzc,is
by subscript
In [7]: p=[' kzc ', 18]in [8]: ' {0[0]},{0[1]} '. Format (P) out[8]: ' kzc,18 '
With these convenient "mapping" methods, we have a lazy weapon. Basic Python knowledge tells us that list and tuple can be "broken" into normal parameters to the function, and dict can be broken into the keyword parameters to the function (through and *). So you can easily pass a list/tuple/dict to the Format function. Very flexible.
Format qualifier
It has a rich "format qualifier" (syntax is {} with:), such as:
Fill and align
Padding is used in conjunction with alignment
^, <, > center, Align Left, right, back with width
: The fill character after the number, only one character, not specified by default is filled with a space
Like what
in [+]: ' {: >8} '. Format (' 189 ') out[15]: ' 189 ' in [+]: ' {: 0>8} '. Format (' 189 ') out[16]: ' 00000189 ' in [+]: ' {: A >8} '. Format (' 189 ') out[17]: ' aaaaa189 '
Accuracy and type F
Accuracy is often used in conjunction with Type F
in []: ' {:. 2f} '. Format (321.33345) out[44]: ' 321.33 '
Where. 2 represents a precision of 2 length, and F represents a float type.
Other types
Mainly in the system, B, D, O, X are binary, decimal, octal, hexadecimal.
In [si]: ' {: b} '. Format (out[54]: ' 10001 ' in []: ' {:d} '. Format (+) out[55]: ' + ' in [+]: ' {: o} '. Format (+) out[56]: ' 21 ' In []: ' {: x} '. Format (out[57]: ' 11 '
Use, the number can also be used to make the amount of thousands separator.
in [+]: ' {:,} '. Format (1234567890) out[47]: ' 1,234,567,890 '
==========================================================================================
Reference 1:vamei Source: Http://www.cnblogs.com/vamei
Reference 2:http://www.jb51.net/article/63672.htm
[Reprint]python string formatting:% operator {} operator