Format description
%d signed integer (decimal)
%i signed integer (decimal)
%o unsigned integer (octal)
%u unsigned integer (decimal)
%x unsigned integer (hexadecimal)
%x unsigned integer (16 uppercase characters)
%e floating-point numbers (scientific notation)
%E floating point number (scientific notation, E instead of e)
%f floating point number (with decimal point symbol)
%F
%g floating-point numbers (%e or%f depending on the size of the value)
%G floating-point number (similar to%G)
%c character and its ASCII code
%r processing objects with the Rper () method
The%s string uses the str () method to process the object, which is better for debug.
Percent percentile sign #就是输出一个%
%p pointer (memory address with hexadecimal print value)
%n the number of stored output characters into the next variable in the parameter list
The% format character can also be used in dictionaries, and the output can be formatted by using% (name) to refer to the elements in the dictionary.
The minus sign indicates that the number should be left-aligned, and "0" tells Python to fill the number with a leading 0, plus the number always displays its positive and negative (+,-) symbol, even if the number is positive.
You can specify the minimum field width, for example: "%5d"% 2. A period character can also be used to specify additional precision, such as "%.3d"% 3.
e.g.
# example: Numeric formatting
nyear = 2018
Nmonth = 8
Nday = 18
# formatted date%02d number to two-bit integer vacancy fill 0
print '%04d-%02d-%02d '% (nyear,nmonth,nday)
>> 2018-08-18# Output results
Fvalue = 8.123
print '%06.2f '%fvalue# reserved 2-bit decimal floating-point with a width of 6
>> 008.12# Output
print '%d '%10# Output Decimal
>> 10
print '%o '%10# output octal
>> 12
print '%02x '%10# output two-bit hex, letter lowercase blank complement 0
>> 0a
print '%04x '%10# output four-bit hex, letter capital Vacancy 0
>> 000A
print '%.2e '%1.2888# Output floating-point type with scientific notation reserved 2 decimal places
>> 1.29e+00
Formatting operator Auxiliary directives
Symbolic effect
* Define width or decimal point accuracy
-Used for left alignment
+ Show plus sign (+) in front of positive number
<sp> show spaces in front of positive numbers
# 0 (' 0 ') is displayed in front of the octal number, preceded by ' 0x ' or ' 0X ' in hexadecimal (depending on
Using ' x ' or ' x ')
0 The number shown is preceded by ' 0 ' instead of the default space
% ' percent ' output a single '% '
(VAR) mapping variable (dictionary parameter)
M.N m is the smallest total width displayed, and n is the number of digits after the decimal point (if available)
Basic methods of Use:
#coding =utf-8 ' can specify the alignment of the desired length of the string:< (default) left > right align ^ middle alignment = (for numbers only) after the decimal point "print ' 1:\t| {0:>10}, '. Format (' Wangyu ') print ' 2:\t| {0:4.2f} '. Format (1.1415926) print ' 3:\t| ', Format (1.1415926, ' <10.2f ') print ' 4:\t| {0:<10},{1:<15} '. Format (' Wangyu ', 1.1415926) print ' 5:\t| User id: {uid} last seen: {last_login} '. Format (uid= ' root ',last_login = ' 5 mar 2008 07:20 ') ' ' Format indicator can contain a presentation type to control the format. For example, a floating-point number can be formatted as a general format or represented by a power. ' B ' - binary. The number is output as a base of 2. ' C ' - characters. Converts an integer to the corresponding Unicode string before printing. ' d ' - decimal integers. The number is output as a base of 10. ' O ' - octal. The number is output as a base of 8. ' x ' - 16 binary. The number is output in 16, and the number of digits above 9 is in lowercase letters. The ' e ' - power symbol. Print numbers in scientific notation. Use ' e ' to indicate power. ' G ' - general format. The value is output in fixed-point format. When the number is particularly large, it is printed in a power form. ' n ' - numbers. When the value is an integer and ' d ', the values are the same as the ' G ' when they are floating-point numbers. The difference is that it inserts a numeric delimiter according to the locale. '% ' - percent. Multiply the value by 100 and then print in fixed-point (' F ') format, followed by a percent semicolon. " print " 6:\t| {0:b} '. Format (3) print ' 7:\t|{0:c} '. Format (3) print ' 8:\t| {0:d} '. Format (3) print ' 9:\t| {0:o} '. Format (3) print ' 10:\t| {0:x} '. Format (3) print ' 11:\t| {0:e} '. Format (3.75) print ' 12:\t| {0:g} '. Format (3.75) print ' 13:\t| {0:n} '. Format (3.75) #浮点数print ' 14:\t| {0:n} '. Format (3) #整数print ' 15:\t| {0:%} '. Format (3.75) #输入形式的控制formata = int (Raw_input (' A: ')) B = int (Raw_input (' B: ')) print ' 16:\t|%*.*f ' % (a, b, 1.1415926) print ' 17:\t| {array[2]} '. Format (Array=range (Ten)) print ' 18:\t| {attr.__class__} '. Format (attr=0) print ' 19:\t| {digit:*^ 10.5f} '. Format (DIGIT=1.0/3) ' "Class and type can define a __format__ () method to control how you format yourself. It will accept a format indicator as a parameter: "' Def __format__ (SELF,&NBSP;FORMAT_SPEC): if isinstance (format_spec, Unicode): return unicode (str (self)) else: return str (self)
650) this.width=650; "src=" Http://static.oschina.net/uploads/space/2013/0528/103002_FCtE_259961.png "alt=" 103002_ Fcte_259961.png "/>
Python string formatting