print格式化

发布时间 2023-07-21 16:06:47作者: 往事已成昨天
s="好好学习"
print("{:25}".format(s))#输出25个字符的宽度,默认左对齐
print("{:>25}".format(s))#输出25个字符的宽度,右对齐
print("{:*^25}".format(s))#输出25个字符的宽度,居中对齐,用*填充
print("{:^1}".format(s))
print("{:^25.3}".format(s))
a=1.235456
print("{:.3}".format(a))
print("{:.2}".format(s))
c=12345565
print("{:+^25,}".format(c))
print("{0:b},{0:c},{0:d},{0:o},{0:x},{0:X}".format(425))
print("{0:e},{0:E},{0:f},{0:%}".format(256))

输出结果如下:

好好学习
好好学习
**********好好学习***********
好好学习
好好学
1.24
好好
+++++++12,345,565++++++++
110101001,Ʃ,425,651,1a9,1A9
2.560000e+02,2.560000E+02,256.000000,25600.000000%

Process finished with exit code 0