python格式化输出(转自White Pillow's Blog)
2015-07-08 11:23
766 查看
Python格式化输出
今天写程序又记不清格式化输出细节了……= =索性整理一下。
python print格式化输出。
1. 打印字符串
效果:
![](http://images.cnitblog.com/i/294206/201405/281715112591828.png)
2.打印整数
效果:
![](http://images.cnitblog.com/i/294206/201405/281715549007593.png)
3.打印浮点数
效果:
![](http://images.cnitblog.com/i/294206/201405/281717280885525.png)
4.打印浮点数(指定保留小数点位数)
效果:
![](http://images.cnitblog.com/i/294206/201405/281719495723934.png)
5.指定占位符宽度
效果:
![](http://images.cnitblog.com/i/294206/201405/281723589006262.png)
6.指定占位符宽度(左对齐)
效果:
![](http://images.cnitblog.com/i/294206/201405/281725436348200.png)
7.指定占位符(只能用0当占位符?)
效果:
![](http://images.cnitblog.com/i/294206/201405/281727152591175.png)
8.科学计数法
效果:
Python格式化输出
今天写程序又记不清格式化输出细节了……= =索性整理一下。
python print格式化输出。
1. 打印字符串
print ("His name is %s"%("Aviad"))
效果:
![](http://images.cnitblog.com/i/294206/201405/281715112591828.png)
2.打印整数
print ("He is %d years old"%(25))
效果:
![](http://images.cnitblog.com/i/294206/201405/281715549007593.png)
3.打印浮点数
print ("His height is %f m"%(1.83))
效果:
![](http://images.cnitblog.com/i/294206/201405/281717280885525.png)
4.打印浮点数(指定保留小数点位数)
print ("His height is %.2f m"%(1.83))
效果:
![](http://images.cnitblog.com/i/294206/201405/281719495723934.png)
5.指定占位符宽度
print ("Name:%10s Age:%8d Height:%8.2f"%("Aviad",25,1.83))
效果:
![](http://images.cnitblog.com/i/294206/201405/281723589006262.png)
6.指定占位符宽度(左对齐)
print ("Name:%-10s Age:%-8d Height:%-8.2f"%("Aviad",25,1.83))
效果:
![](http://images.cnitblog.com/i/294206/201405/281725436348200.png)
7.指定占位符(只能用0当占位符?)
print ("Name:%-10s Age:%08d Height:%08.2f"%("Aviad",25,1.83))
效果:
![](http://images.cnitblog.com/i/294206/201405/281727152591175.png)
8.科学计数法
format(0.0015,'.2e')
效果:
![](http://images.cnitblog.com/i/294206/201407/241348457447790.png)
相关文章推荐
- 浅析C语言字中的符串格式化显示
- Linux下date命令,格式化输出,时间设置方法
- C语言之格式化输出
- python格式化输出符号及大量案例
- String format 格式化字符串
- Java Appeandable接口与字符串格式化
- Python3基础(七) I/O操作
- Python - "print()"函数 中 "%" 后面字母的意义
- Python - 格式化(format())输出字符串 详解 及 代码
- C++ - 格式化输入输出(formatted input and output) 的 详解 及 代码
- C++ 格式化输出
- 关于C中格式化输出的一些笔记
- fprintf文件格式化输出遇到的问题及其解决
- Python - "print()"函数 中 "%" 后面字母的意义
- 30天30个Golang程序学习(一):格式化输出
- java xml 格式化输出
- java如何将json数据格式化输出到控制台
- python-开发之路-格式化输入
- 格式化输出width,fill
- NSLog 输出格式