python格式化输出【转】
- >>> print 'i am %d years old'%25
- i am 25 years old


- >>> num=10
- >>> print'dec=%d, oct=%o, hex=%x'%(num,num,num)
- dec=10, oct=12, hex=a
2、浮点数输出
- >>> f=3.1415
- >>> print 'pi=%f'%f
- pi=3.141500
- >>> f=3.141500000
- >>> print 'pi=%f'%f
- pi=3.141500

- >>> numb1=0.000033333
- >>> print '%f, %e, %g'%(numb1,numb1,numb1)
- 0.000033, 3.333300e-05, 3.3333e-05
- >>> numb2=0.3333
- >>> print '%f, %e, %g'%(numb2,numb2,numb2)
- 0.333300, 3.333000e-01, 0.3333

- >>> numb1=0.000003333333
- >>> print '%.3f, %.3e, %.3g'%(numb1,numb1,numb1)
- 0.000, 3.333e-06, 3.33e-06
- >>> numb3=1234567.1234567
- >>> print '%f, %e, %g'%(numb3,numb3,numb3)
- 1234567.123457, 1.234567e+06, 1.23457e+06
- >>> print '%.3f, %.3e, %.3g'%(numb3,numb3,numb3)
- 1234567.123, 1.235e+06, 1.23e+06

- >>> round(2.3)
- 2.0
- >>> round(2.5)
- 3.0
- >>> round(2.7)
- 3.0
- >>> round(2.567)
- 3.0


- >>> round(2.5555,3)
- 2.555
- >>> round(2.456,2)
- 2.46
- >>> round(2.665,2)
- 2.67
- >>> round(2.675,2)
- 2.67
- >>> round(2.655,2)
- 2.65
- >>> round(2.635,2)
- 2.63
- >>> print 'i love %s'%'python'
- i love python

- >>> print 'i love %.2s'%'python'
- i love py

- >>> print 'i love %s'%'python'
- i love python
- >>> print 'i love %.2s'%'python'
- i love py
- >>> print 'i love %3.2s'%'python'
- i love py
- >>> print 'i love %10.2s'%'python'
- i love py
- >>> print 'i love %-10.2s'%'python'
- i love py
- >>> print 'i love %-10.2s !'%'python'
- i love py !

- >>> fmt='%10s%10s%10s'
- >>> print fmt%('name','age','sex')
- name age sex
- >>> fmt='%-10s%-10s%-10s'
- >>> print fmt%('name','age','sex')
- name age sex
- >>> print fmt%('sqxu',25,'boy')
- sqxu 25 boy
转义字符
\(在行尾时)
\\
\'
\"
\a
\b
\e
\000
\n
\v
\t
\r
\f
\oyy
\xyy
\other
二、format()函数
相对基本格式化输出采用‘%’的方法,format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号‘{}’作为特殊字符代替‘%’。(1)通过位置替换
- >>> print '{0} {1}'.format('hello','world')
- hello world
- >>> print '{} {}'.format('hello','world')
- hello world
- >>> print '{0} {1} {0}'.format('hello','world')
- hello world hello
print 'i love {python}'.format(python='you')
- i love you
(3)其他使用方法如:可以指定输出长度和输出的对齐方式,其中对齐方式有一下几种:< (默认)左对齐> 右对齐
- >>> print format('string','2s')
- string
- >>> print format(3.14151617,'.5f')
- 3.14152
- >>> print '{0:>10}'.format('sqxu') #10个占位符,右对齐
- sqxu
- >>> print '{0:4.2f}'.format(3.141516)
- 3.14
- >>> print '{0:6.2f}'.format(3.141516)
- 3.14
- >>> print '{0:>6.2f}'.format(3.141516)
- 3.14
- >>> print '{1:<10},{0:<15}'.format('sqxu','USTC')
- USTC ,sqxu
- >>> print 'name={name},age={age}'.format(name='sqxu',age=25)
- name=sqxu,age=25
同上述格式化输出一样,也可以通过格式化指示符来控制格式。例如,浮点数可以被格式化为一般格式或用幂来表示。'b' - 二进制。将数字以2为基数进行输出。'c' - 字符。在打印之前将整数转换成对应的Unicode字符串。'd' - 十进制整数。将数字以10为基数进行输出。'o' - 八进制。将数字以8为基数进行输出。'x' - 十六进制。将数字以16为基数进行输出,9以上的位数用小写字母。'e' - 幂符号。用科学计数法打印数字。用'e'表示幂。'g' - 一般格式。将数值以fixed-point格式输出。当数值特别大的时候,用幂形式打印。'n' - 数字。当值为整数时和'd'相同,值为浮点数时和'g'相同。不同的是它会根据区域设置插入数字分隔符。'%' - 百分数。将数值乘以100然后以fixed-point('f')格式打印,值后面会有一个百分号。
- >>> print '{0:b}'.format(3)
- 11
- >>> print '{0:c}'.format(30)
- >>> print '{0:d}'.format(3)
- 3
- >>> print '{0:o}'.format(10)
- 12
- >>> print '{0:x}'.format(30)
- 1e
- >>> print '{0:e}'.format(3)
- 3.000000e+00
- >>> print '{0:f}'.format(3)
- 3.000000
- >>> print '{0:g}'.format(3)
- 3
- >>> print '{0:n}'.format(3)
- 3
- >>> print '{0:n}'.format(3.1415)
- 3.1415
- >>> print '{0:%}'.format(3.15)
- 315.000000%
format()函数还有其他一些应用,使用起来也很方便,在此不一一赘述。转自python格式化输出 - CSDN博客 http://blog.csdn.net/wchoclate/article/details/42297173
python格式化输出【转】的更多相关文章
- Python格式化输出的三种方式
Python格式化输出的三种方式 一.占位符 程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式比如要求用户输入用户名和年龄,然后打印如下格式:My name is xxx,my age ...
- python格式化输出及大量案例
python格式化输出符号及大量案例 1.格式化输出符号 python格式化输出符号 格式化符号 含义 %c 转化成字符 %r 优先使用repr()函数进行字符串转化 %s 转换成字符串,优先使用st ...
- Python 格式化输出
转载 今天写程序又记不清格式化输出细节了--= =索性整理一下. 注意: 与C/C++ 不同的是这里括号后面不需要加' , '号. python print格式化输出. 1. 打印字符串 print ...
- Python格式化输出
今天写程序又记不清格式化输出细节了……= =索性整理一下. python print格式化输出. 1. 打印字符串 print ("His name is %s"%("A ...
- [No000063]Python格式化输出
python print格式化输出. 1. 打印字符串 print ("His name is %s"%("Aviad")) 效果: 2.打印整数 print ...
- [转]Python格式化输出
今天写程序又记不清格式化输出细节了……= =索性整理一下. python print格式化输出. 1. 打印字符串 print ("His name is %s"%("A ...
- Python学习教程(learning Python)--1.2.2 Python格式化输出基础
本节讨论为何要格式化输出数据? 先看一段代码吧,本程序的功能是计算月支付金额. amount_due = 5000.0 #年支付金额 monthly_payment = amount_due / 12 ...
- Python格式化输出%s和%d
python print格式化输出. 1. 打印字符串 print ("His name is %s"%("Aviad")) 效果: 2.打印整数 print ...
- Python 格式化输出 —— 小数转化为百分数
比如将 0.1234 转化为 12.34% 的形式: rate = .1234 print('%.2f%%' % (rate * 100)) 第一个百分号和 .2f 相连,表示浮点数类型保留小数点后两 ...
随机推荐
- BAE静态文件问题
这几天想在bae上架一个自己的博客,但是老是访问不到静态文件文件,都要没有办法了,最后看了这篇博客,受到了启发,知道了问题所在: 我自己的原始的app.conf的配置如下: handlers: - u ...
- Final发布点评
1. 约跑App——nice!:为改进演示效果,本组使用摄像头实时采集投影的方式展示其作品,是一种演示的创新.本组重点放在了修改上次来着其他组发现的bug,不过新功能上好像没有加入多少,可能是保证软 ...
- 2013长春网赛1004 hdu 4762 Cut the Cake
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4762 题意:有个蛋糕,切成m块,将n个草莓放在上面,问所有的草莓放在同一块蛋糕上面的概率是多少.2 & ...
- IdeaVim-常用操作(转载)
IdeaVim简介 IdeaVim是IntelliJ IDEA的一款插件,他提高了我们写代码的速度,对代码的跳转,查找也很友好. 安装位置 安装之后它在 Tools > Vim Emulator ...
- 常用的Hql语句
// HQL: Hibernate Query Language.// 特点:// >> 1,与SQL相似,SQL中的语法基本上都可以直接使用.// >> 2,SQL查询的是表 ...
- 常用的sublime text 3插件(很爽哦)
个人比较懒,平时喜欢用webstorm,但是因为webstorm打开实在太慢了,并且太看设备,所以本人编辑简单的文件依然会选择使用sublime,虽然网上有很多关于此类插件的分享了,但是感觉都是片段, ...
- BZOJ4066 简单题(KD-Tree)
板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- linux下彻底卸载mysql 图解教程
linux下彻底卸载mysql 图解教程 1.查找以前是否装有mysql 命令:rpm -qa|grep -i mysql可以看到如下图的所示: 说明之前安装了:MySQL-client-5.5.25 ...
- 【BZOJ1862】[ZJOI2006]游戏排名系统 (Splay)
[BZOJ1862][ZJOI2006]游戏排名系统 (Splay) 题面 BZOJ 洛谷 题解 双倍经验题
- emWin 界面切换注意事项
@2018-07-10 emWin 在做界面切换时,须将切换前的界面所有信息 “删除”,否则将造成切换后的界面死机 此 “删除” 对象包括: > 界面上绘制的曲线(随时间一直变化).绘制的2D ...