立即学习:https://edu.csdn.net/course/play/26755/340116?utm_source=blogtoedu
Format函数格式化常解:
参数:
第一个参数表示要用来格式化的数值; 第二个参数表示格式化的规则
常用格式化用法:
s1 = 123
# 左对齐 >
print(format(s1, '10d')) # 默认空格填充
print(format(s1, '*>10d')) # 使用* 填充
# 右对齐 <
print(format(s1, '0<10d')) # 使用0 填充
# 中心对齐 ^
print(format(s1, '#^10d')) # 使用# 填充
# 科学计数法 e(E)
s2 = 123456789
print(format(s2, 'e'))
print(format(s2, '0.2E')) # 组合使用
# 千位号(,) 分割
s3 = 12345.6789
print(format(s3, ','))
print(format(s3, ',.2f')) # 组合使用