% placeholder is a placeholder that is often used in python programs.
Its role is to occupy a position for the following variable value
For example, to format the output of the nine-nine multiplication table, the % placeholder is used
# Ninety-nine multiplication tableprint('Nine-Nine Multiplication Table')for i in range(1, 11):for j in range(1, i +1):print('%dx%d=%d\t'span> % (j, i, j * i), end="")print("")
Result:
%d integer used above(int) placeholder
% placeholders commonly used in python programs
1, %s, string placeholder, use str() method to convert any Python object, including integer int, floating point float, such as:
print('%s,%s,%s'%('string',10,1.0))
Result:
2, %d, integer (int)A placeholder, which can also represent a floating-point number float (only the integer part is taken), such as:
print('represents an integer: %d, represents a floating point number: %d'%(1,1.9))
Result:
3, %f, float (float) placeholder, which can also represent an integer (int), which are reserved for 6 decimal places by default, such as:
print('represents a floating point number: %f, represents an integer: %f'%(1.234,1))
Result:
Problem: If we don't want to keep 6 decimalsBit, I want to define how many bits to keep, how to write it?
Solution: add the number of digits to be reserved after %
Format: %.nf, n means to retain n decimal places
print('Keep 2 decimal places: %.2f'%1.2345)
Result: