python print格式化输出
来源:互联网 发布:日本漫画软件下载 编辑:程序博客网 时间:2024/05/02 00:38
一、速查手册
1.字符串格式化代码:
2.可以指定所需长度的字符串的对齐方式:
< (默认)左对齐
> 右对齐
^ 中间对齐
= (只用于数字)在小数点后进行补齐
3.格式化指示符
'b' - 二进制。将数字以2为基数进行输出。
'c' - 字符。在打印之前将整数转换成对应的Unicode字符串。
'd' - 十进制整数。将数字以10为基数进行输出。
'o' - 八进制。将数字以8为基数进行输出。
'x' - 十六进制。将数字以16为基数进行输出,9以上的位数用小写字母。
'e' - 幂符号。用科学计数法打印数字。用'e'表示幂。
'g' - 一般格式。将数值以fixed-point格式输出。当数值特别大的时候,用幂形式打印。
'n' - 数字。当值为整数时和'd'相同,值为浮点数时和'g'相同。不同的是它会根据区域设置插入数字分隔符。
'%' - 百分数。将数值乘以100然后以fixed-point('f')格式打印,值后面会有一个百分号。
二、案例
# 打印字符串print ("His name is %s" % ("Aviad"))#His name is Aviad# 打印整数print ("He is %d years old" % (25))#He is 25 years old<pre name="code" class="python">print ("He is %d years old" % (25))#He is 25 years old# 打印浮点数print ("His height is %f m" % (1.83))#His height is 1.83 m# 打印浮点数(指定保留小数点位数)print ("His height is %.2f m" % (1.83))#His height is 1.83 m"print "His height is ", format(1.83, '.2f'), "m"# 指定占位符宽度print ("Name:%10s Age:%8d Height:%8.2f" % ("Aviad",25,1.83))#Name:Aviad Age: 25 Height: 1.83# 指定占位符宽度(左对齐)print ("Name:%-10s Age:%-8d Height:%-8.2f" % ("Aviad",25,1.83))#Name:Aviad Age:25 Height:1.83# 指定占位符(用0或者空格当占位符)print ("Name:%-10s Age:%08d Height:%08.2f" % ("Aviad",25,1.83))#Name:Aviad Age:00000025 Height:00001.83# 调用format函数,format(数值, '格式')print('test:{0:10f}'.format(math.pi))#test: 3.141593#若输出位数小于10,则默认右对齐。若输出位数大于宽度,则按实际位数输出format(0.0015,'.2e')#1.50e-03# 格式化指示符print '6:\t|{0:b}'.format(3)print '7:\t|{0:c}'.format(3)print '8:\t|{0:d}'.format(3)print '9:\t|{0:o}'.format(3)print '10:\t|{0:x}'.format(3)print '11:\t|{0:e}'.format(3.75)print '12:\t|{0:g}'.format(3.75)print '13:\t|{0:n}'.format(3.75) #浮点数print '14:\t|{0:n}'.format(3) #整数print '15:\t|{0:%}'.format(3.75)
三、总结
1.两种输出风格
python一共有两种格式化输出语法
一种是类似于C语言printf的方式,称为 Formatting Expression
- >>>print '%s %d-%d' % ('hello', 7, 1)
- 'hello 7-1'
- >>> print ("He is %d years old" % (25))
- 'He is 25 years old'
基本是前文格式控制,%(,,)中分别表示输出内容。
另一种是类似于C#的方式,称为String Formatting Method Calls
- >>>print '{0} {1}:{2}'.format('hello', '1', '7')
- 'hello 1:7'
- >>>print 'test:{0:10f}'.format(math.pi)
'test: 3.141593'
基本是前文格式控制,.format(,,)中分别表示输出内容。数字(0, 1, ...)即代表format()里面的元素, 所以可以使用.调用元素的方法;
字符串的参数使用{NUM}进行表示,0, 表示第一个参数,1, 表示第二个参数, 以后顺次递加;
使用:, 指定代表元素需要的操作, 如:.3小数点三位, :8占8个字符空间等;
format()函数的用法:它通过{}和:来代替%。
a='is %s' % (1)b='is {0}'.format(1)
字符串格式化常用% 就是C里的printf
format是python 字符串自己的方法, 推荐用format,因为比较灵活
%() 等同于.format(),括号内是输出内容
前者采用C风格,后者采用C#风格
1 0
- Python print 格式化输出
- python print格式化输出
- python print格式化输出
- Python print格式化输出
- python print格式化输出
- Python print函数用法,print 格式化输出(python格式化输出)
- Python print函数用法,print 格式化输出
- Python print函数用法,print 格式化输出
- Python print函数用法,print 格式化输出
- Python print函数用法,print 格式化输出
- Python print函数用法,print 格式化输出
- Python print函数用法,print 格式化输出
- Python print函数用法,print格式化输出
- Python print函数用法,print 格式化输出
- Python print函数用法,print 格式化输出
- Python print函数用法,print 格式化输出
- Python print函数用法,print 格式化输出
- Python print函数用法,print 格式化输出
- oracle基础学习
- js金额计算解决精度问题
- Toolbar的用法,实现导航栏的切换
- HDU_1087_SuperJumping!Jumping!Jumping!
- 如何提高tableView的性能
- python print格式化输出
- 文本文件加密解密(一)
- Android ViewPager多页面滑动切换以及动画效果
- UnicodeEncodeError: 'gbk' codec can't encode character '\ufeff' in position 0: illegal multibyte seq
- 树莓派-WebCamera图像采集(OpenCV)
- 使用 ftrace 调试 Linux 内核,第 1 部分
- 使用dbcp连接池连接数据库
- genymotion-unable to load virtualbox engine-Windows
- mysql备份