The Python string is formatted with the character% format 1 format 2 character% (variable 1, variable 2), and the% format represents the type that accepts the variable. Examples of simple uses are as follows
# Example: String formatting
Name = ' 17jo '
print ' www.%s.com '%name
>> www.111cn.net
Name = ' 17jo '
Zone = ' com '
print ' www.%s.%s '% (name,zone)
>> www.111cn.net
The string is formatted with a different format symbol, representing the different types to be converted, as shown below.
Format Symbol Presentation type
%s string
%d/%i decimal Integer
%u decimal integers
%o octal integers
%x/%x hexadecimal integer
%e/%e Science Count
%f/%f floating-point numbers
%% OUTPUT%
When the format symbol is a number, it can be preceded by an amount and a fill position such as:%[0][total digits [.] [Decimal places] to set the style to be converted, using the following methods:
# Example: Number formatting
nyear = 2018
Nmonth = 8
Nday = 18
# format Date%02d number into two-bit integer vacancy 0
print '%04d-%02d-%02d '% (nyear,nmonth,nday)
>> 2018-08-18 # Output results
Fvalue = 8.123
print '%06.2f '%fvalue # preserves 2-bit decimal floating-point type with width 6
>> 008.12 # Output
print '%d '%10 # output decimal
>> 10
print '%o '%10 # output octal
>> 12
print '%02x '%10 # output two-bit hexadecimal, letter lowercase blank fill 0
>> 0a
print '%04x '%10 # output four-bit hex, letter-Uppercase vacancy supplement 0
>> 000A
print '%.2e '%1.2888 # with scientific notation output floating-point type keep 2 decimal digits
>> 1.29e+00
We have shown how to replace the specified field. We can also format it precisely by adding a colon after the format indicator. For example:
Python:format
# Field 0:left justify, pad to characters
# Field 1:right Justify, pad to 6 characters
FMT = ' {0:15} ${1:>6} '
Fmt.format (' registration ', km)->
' Registration $35 '
Fmt.format (' Tutorial ', m)->
' Tutorial $50 '
Fmt.format (' Banquet ',->)
' Banquet $125 '
Formatting indicators can be referenced by nesting.
FMT = ' {0:{1}} '
width = 15
Fmt.format (' Invoice #1234 ', width)->
' Invoice #1234 '
width = 35
Fmt.format (' Invoice #1234 ', width)->
' Invoice #1234 '
You can specify the alignment of the string for the desired length.
Effect character:
< (default) left-aligned
> Right Alignment
^ Center Alignment
= (for numbers only) to be padded after the decimal point
A format indicator can contain a display type to control the format. For example, floating-point numbers can be formatted as a general format or represented by a power.
>>> ' {0:g} '. Format (3.75)
' 3.75 '
>>> ' {0:e} '. Format (3.75)
' 3.750000e+00 '
There are a lot of display types. 2.6 has a complete list in the document. Some examples are listed here.
' B '-binary. Outputs a number as a base of 2.
' C '-character. Converts an integer to the corresponding Unicode string before printing.
' d '-decimal integer. Outputs a number as a base of 10.
' O '-eight into the system. Outputs a number as a base of 8.
' x '-16 system. The number is output in 16 base, and more than 9 digits are in lowercase letters.
' E '-power symbol. Print numbers with scientific notation. Use ' e ' to represent power.
' G '-general format. Outputs a numeric value in fixed-point format. When the value is particularly large, print in power form.
' N '-number. When the value is an integer, it is the same as ' d ', and is the same as ' G ' when the floating-point number is. The difference is that it inserts a number separator based on the locale setting.
'% '-percent. Multiply the number by 100 and print in fixed-point (' F ') with a percent semicolon after the value.
Classes and types can define a __format__ () method to control how you format yourself. It takes a format indicator as an argument:
def __format__ (self, Format_spec):
If Isinstance (Format_spec, Unicode):
return Unicode (str (self))
Else
Return str (self)
There is also a built-in format () method that can be used to format a value. It invokes the __format__ () method of the type and passes the format indicator in as a parameter.
>>> format (75.6564, '. 2f ')
' 75.66 '