Python 通过format()函数输出字符串左对齐、右对齐、居中对齐

通过format()函数格式化实现左对齐、居中、右对齐

我们通过一道例题来看

编写Python程序计算下列数学表达式的结果并输出,小数点后保留3位,输出占10位,空位用‘*’填充,右对齐。
在这里插入图片描述

#代码:
x=pow(3**4+5*(6**7)/8,0.5)
print(format("{:.3f}".format(x),"*<10"))
print(format("{:.3f}".format(x),"*>10"))
print(format("{:.3f}".format(x),"*^10"))

#输出结果
418.379***
***418.379
*418.379**
发布了47 篇原创文章 · 获赞 7 · 访问量 4252

猜你喜欢

转载自blog.csdn.net/LiuLong0907/article/details/105097623