Usage:
It uses {} and: Instead of the traditional% way
1. Using Position parameters
Important: From the following example, we can see that the positional parameters are not constrained by order, and can be {}, as long as there is a corresponding parameter value in format, the parameter index is open from 0, the incoming position parameter list is available * list
>>> Li = ['HoHo', 18]>>>'my name is {}, age {}'. Format ('HoHo', 18)'My name is HoHo'>>>'My name is {1}, age {0}'. Format (10,'HoHo')'My name is HoHo, age'>>>'My name is {1}, age {0} {1}'. Format (10,'HoHo')'My name is HoHo, age HoHo'>>>'my name is {}, age {}'. Format (*Li)'My name is HoHo'
2. Use keyword parameters
IMPORTANT: Keyword parameter value to be right, available dictionary when the keyword parameter passed in the value, the dictionary before adding * * can
>>> hash = {'name':'HoHo',' Age': 18}>>>'My name is {Name},age are {age}'. Format (name='HoHo', age=19)'My name is Hoho,age'>>>'My name is {Name},age are {age}'. Format (* *hash)'My name is Hoho,age'
3. Filling and formatting
: [padding character] [alignment <^>][width]
' {0:*>10} '. Format (ten) # #右对齐 '********10'{0:*<10} '. Format (ten) # #左对齐 '10********'{0: *^10}'. Format (ten) # #居中对齐 '****10****'
4, precision and the input system
>>>'{0:.2f}'. Format (1/3)'0.33'>>>'{0:b}'. Format (10)# Binary'1010'>>>'{0:o}'. Format (10)#octal' A'>>>'{0:x}'. Format (10)#16 binary'a'>>>'{:,}'. Format (12369132698)#Thousands of bits formatted'12,369,132,698'
5. Use index
>>> li['hoho', +]'name is {0[0]}-age is {0 [1]} ' . Format (LI) ' name is HoHo
Python string formatting (format)