1. Numerical types (int, float)
# %d, %f are placeholders
>>> a = 3.1415926
>>> print("%d"%a) #%d can only output integers, int class
3
>>> print("%f"%a ) #%f output floating point number
3.141593
>>> print("%.2f"%a) #output decimal places
3.14 as required
>>> print("%.9f"%a) #if the required number of decimal places is too large More, then use 0 to complete
3.141592600
>>> b = 3
>>> print("%4d"%b) #If it is an integer, this requires the integer to occupy four positions, so add three spaces in front of
it 3 #Instead of writing it in the style of 0003
>>> print("%06d"%int(a)) #Display of the integer part requires a total of 6 digits to be displayed. If the integer has less than 6 digits, the missing digits are filled with zeros in front of the integer.
000003
>>> print('%06d'%b)
000012
In actual programming, we often need to write a=xxx style to make the output interface more friendly. Therefore, the author also copied a piece of code from the author of the original link, and slightly modified it to give the expression paradigm of Python3.
(1) Right-aligned
>>> print("PI=%10.3f"%a) #Constrain, the meaning of this is that the integer part plus the decimal point and the decimal part total 10 digits, and right-aligned
PI= 3.142
(2) Left alignment
>>> print("PI=%-10.3f"%a) #The left alignment is required to be displayed, and the rest are the same as above
PI=3.142
Second, the character type (str)
Similar to numeric types, but the placeholders of %d and %f are changed to placeholders of %s.
Reprinted from: https://www.cnblogs.com/vanly/p/5589373.html