学习目标:
①格式化整数
②格式化浮点数
③描述format函数
- Python日常
内容展示:
①:格式化整数
n = 12234
print(format(n, '10d')) # 12234
print(format(n, '0>10d')) # 左側 0000012234
print(format(n, '0<10d')) # 右側 1223400000
②:格式化浮点型
x1 = 1234.5677
x2 = 30.1
print(format(x1, '0.2f')) # 保留小数点后两位 1234.57
print(format(x2, '.2f')) # 保留小数点后两位 30.10
③:format函数描述
x1 = 1234.5677
x2 = 30.1
print(format(x2, '*>15.4f')) # 右对齐,********30.1000
print(format(x2, '*<15.4f')) # 左对齐 30.1000********
print(format(x2, '*^15.4f')) # 中心对齐 ****30.1000****
print(format(123456789, ',')) # 用千分号分隔 123,456,789
print(format(123352.76681781, ',.2f')) # 123,352.77
print(format(x1, 'e')) # 1.234568e+03
print(format(x1, '0.2e')) # 1.23e+03
总结:
右对齐 print(format(s, ‘>15.4f’)) # 15:输出一共十五位 .4:小數点后保留 四位
左对齐 print(format(s, '<15.4f’))
中间对齐 print(format(s, ‘*^15.4f’))
千分号分隔 print(format(s, ‘,’))
千分号加小数点限制 print(format(s, ‘,.2f’))
- CSDN Python日常 第8 篇
- 下一篇链接