The print function of Python3.x version is aligned left and right

The case of numbers:

a = 5 , b = 5.2,c = "123456789"

The most common right alignment: print("%3d"%a) outputs 5 (details: two spaces before 5)

print("%10.3f"%b) outputs 5.200 (details: 10 means the entire output occupies 10 spaces, the decimal point space is also counted, 3 means there are three digits after the decimal point, if not enough, add 0)

print("%.3f"%b) outputs 5.200 (details: it means that there are three digits after the decimal point, if it is not enough, it will be filled with 0)

String case:

Same as numbers, except %s is used instead of %d, %f

print("%.3s"%c) outputs 123

print("%10.3s"%c) outputs 123 (details: 7 spaces before 1)


In the case of left alignment, just print("%-3d"%a) will do.


Personally, I feel that the print function has some similarities with the printf format in the c language.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325516345&siteId=291194637