Talking about the python-print

A numerical type (int, float)

#% D,% f is a placeholder
>>> = 3.1415926 A
>>> Print ( "% D" A%) #% D output only integers, int type
. 3
>>> Print ( "% F"% A ) #% f output float
3.141593
>>> Print ( "%.. 2F"% a) according to the required output decimals #
3.14
>>> Print ( "%. 9F" a%) # decimals if required through and more, back on the use of 0-completion
3.141592600

 


B = 3 >>>          
>>> Print ( "4D%"% B) # If are integers such that the integer write requests occupies four positions, thus increasing the space before the three
   3 # 0003 written in a style instead of

 

>>> print ( "% 06d"% int (a)) # integer portion of the display, the display requires a total of six. If the integer less than six digits, then make up less than in the previous integer with 0.
000003

>>> print('%06d'%b)
000012

 

In actual programming, we often need to write a = xxx styles, the output interface more friendly. So it also copies the link to the original source of a piece of code, slightly modified to give expression of paradigm Python3.

(1) right justified

>>> print ( "PI =% 10.3f "% a) # what constraints, this is the meaning of an integer part and a fractional part of the decimal point plus the total of 10, and the right-aligned
PI = 3.142

(2) Left
>>> print ( "PI =% - 10.3f"% a) # required to display the left-justified, as with the above remaining
PI = 3.142

 

Second, the character type (str)

Numeric types and the like, but the% d,% f becomes a placeholder placeholder% s.

 

Transfer: https: //www.cnblogs.com/vanly/p/5589373.html

 

 

I have written the following to write a little code demonstrates:

2 print data corresponding to the list.

Guess you like

Origin www.cnblogs.com/godwall/p/12036168.html