First, the output is formatted by%.
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.
#common method of transmitting valuePrint('name:%s,age:%d'%('MZC', 20)) Output: Name:mzc,age:20#use Dict for value transferPrint('My name is% (name) S,age% (age) D'%{'name':'Jerry',' Age': 20})
Output: My name is Jerry,age 20
Print floating-point numbers, and choose to retain the number of decimal digits
The number of decimal digits reserved is determined by the output control%.XF x is the number of reserved digits
# floating point number does not change printing Print ('width is%f' %1.337) Output: width is 1.337000 # Select the number of decimal digits to keep Print ('width is%.2f' %1.345)
Output: Width is 1.34
Specify the placeholder width:
Print ("name:%10s age:%8d height:%8.2f"% ("aviad", 25,1.83))
Output effect: Name:aviad age:25 height:1.83
Specify the placeholder width (left-justified)
Print ("name:%-10s age:%-8d height:%-8.2f"% ("aviad", 25,1.83))
Output effect: Name:aviad age:25 height:1.83
Specify placeholders (only 0 when placeholders are used). )
Print ("name:%-10s age:%08d height:%08.2f"% ("aviad", 25,1.83))
Output effect: Name:aviad age:00000025 height:00001.83
Scientific counting method
Format (0.0015,'. 2e')
Output effect: ' 1.50e-03 '
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 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:.
The format output control is a mapping relationship.
1, through the location
Print ('{0},{1}'. Format ('KZC', 18))output: 'kzc,18'
Print ('{},{}'. Format ('KZC', 18))Output:'kzc,18'
Print ('{1},{0},{1}'. Format ('KZC', 18)) output: '18,kzc,18'
2, through the key words
print ('{name},{age}'. Format (age=18,name='kzc' ) output: 'kzc,18'
3. Through Object properties
class Person : def __init__ (self,name,age): = Name,age def__str__(self): return' This was {self.name},is {self.age} old '
str(Person(‘kzc‘,18))
Output: ' This guy is Kzc,is
4, through the subscript
p=['kzc', +]print ('{0[0]},{0[1]}'. Format (P) )
output Result: ' 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.
Print ('My pet name is {student[0]}'. Format (student= is TomPrint ('my pet {D[tom]} yearsold'. Format (d=2 years old
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
Print ('{: >8}'. Format ('189'))Output:'189'Print ('{: 0>8}'. Format ('189'))Output:'00000189'Print ('{: a>8}'. Format ('189'))Output:'aaaaa189'
Accuracy and type F
Accuracy is often used in conjunction with Type F
priint ('{:. 2f}'. Format (321.33345)) ' 321.33 '
Other types
Mainly in the system, B, D, O, X are binary, decimal, octal, hexadecimal.
Print ('{: b}'. Format (17))Output:'10001'Print ('{:D}'. Format (17))Output:' -'print '{: o}'. Format (17)Output:' +'Print ('{: x}'. Format (17))Output:' One'
Use, the number can also be used to make the amount of thousands separator.
print ('{:,}'. Format (1234567890)) ' 1,234,567,890 '
Some of the content has been modified, the article source: http://www.jb51.net/article/108193.htm
Python formatted string