Python 格式化输出 %

demo.py:


name = "张三"
print("我的名字叫%s" %name)  # %s 字符串

student_no = 26
print("我的学号是%06d" %student_no)  # %06d 至少6位,不足6位的用0补齐。

price = 8.5
weight = 7.5
money = price * weight
# %.2f 保留2位小数
print("单价 %.2f ,购买了 %.3f 斤,需要支付 %.4f 元" %(price, weight, money))

scale = 0.8
print("数据比例是 %.2f%%" %(scale * 100))  # %% 输出%

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/83821702