今天写程序又记不清格式化输出细节了……= =索性整理一下。

python print格式化输出。

1. 打印字符串

print ("His name is %s"%("Aviad"))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

2.打印整数

print ("He is %d years old"%(25))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

3.打印浮点数

print ("His height is %f m"%(1.83))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

4.打印浮点数(指定保留小数点位数)

print ("His height is %.2f m"%(1.83))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

5.指定占位符宽度

print ("Name:%10s Age:%8d Height:%8.2f"%("Aviad",25,1.83))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

6.指定占位符宽度(左对齐)

print ("Name:%-10s Age:%-8d Height:%-8.2f"%("Aviad",25,1.83))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

7.指定占位符(只能用0当占位符?)

print ("Name:%-10s Age:%08d Height:%08.2f"%("Aviad",25,1.83))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

8.科学计数法

format(0.0015,'.2e')

效果:

[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

 
编程

今天写程序又记不清格式化输出细节了……= =索性整理一下。

python print格式化输出。

1. 打印字符串

print ("His name is %s"%("Aviad"))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

2.打印整数

print ("He is %d years old"%(25))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

3.打印浮点数

print ("His height is %f m"%(1.83))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

4.打印浮点数(指定保留小数点位数)

print ("His height is %.2f m"%(1.83))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

5.指定占位符宽度

print ("Name:%10s Age:%8d Height:%8.2f"%("Aviad",25,1.83))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

6.指定占位符宽度(左对齐)

print ("Name:%-10s Age:%-8d Height:%-8.2f"%("Aviad",25,1.83))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

7.指定占位符(只能用0当占位符?)

print ("Name:%-10s Age:%08d Height:%08.2f"%("Aviad",25,1.83))

效果:
[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

8.科学计数法

format(0.0015,'.2e')

效果:

[转]Python格式化输出
    





		
来源:https://www.cnblogs.com/plwang1990/p/3757549.html

相关文章:

  • 2022-02-09
  • 2022-03-01
  • 2022-12-23
  • 2021-11-12
  • 2021-04-08
  • 2021-06-28
  • 2021-07-15
  • 2021-07-24
猜你喜欢
  • 2021-12-25
  • 2021-04-15
  • 2022-12-23
  • 2022-12-23
  • 2022-03-05
  • 2021-09-30
  • 2021-11-03
相关资源
相似解决方案