1. 程式人生 > >Python 格式化輸出 %

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))  # %% 輸出%