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 相连,表示浮点数类型保留小数点后两 ...
随机推荐
- asp.net简述Web Forms开发模式
详情请查阅:http://www.runoob.com/aspnet/aspnet-intro.html 1.Web Forms 是三种创建 ASP.NET 网站和 Web 应用程序的编程模式中的一种 ...
- 一条sql语句搞定基于mysql的sql执行顺序的基本理解
对数据库基本操作是每个程序员基本功,如何理解并快速记住sql执行的顺序呢,其实一条复杂的sql就能搞定: SELECT DISTINCT <select_list> FROM <le ...
- KSOA单据保护表中Clientid解析为mac和ip
SELECT DISTINCT a.*,ISNULL(c.client_net_address,'') AS client_net_address FROM ( SELECT * ,),,) ) ), ...
- EntityFramework异常The specified cast from a materialized 'System.Double' type to the 'System.Single' type is not valid.
实体类: public class ReportEntity { public string FactorName { get; set; } public double MaxVal { get; ...
- 1643【例 3】Fibonacci 前 n 项和
1643:[例 3]Fibonacci 前 n 项和 时间限制: 1000 ms 内存限制: 524288 KB sol:这题应该挺水的吧,就像个板子一样 1 0 01 1 0 * ...
- NOI Linux的安装说明以及使用指南
安装 本人的安装环境为Win10. 1. 首先从官网上下载一个CCF官方提供的Noi linux虚拟机以及安装文档 传送门 2. 然后,安装一个VMware Workstation 14 Pro,这里 ...
- C++原型模式和模板模式
DP书上的定义为:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.其中有一个词很重要,那就是拷贝.可以说,拷贝是原型模式的精髓所在.举个现实中的例子来介绍原型模式.找工作的时候,我们需 ...
- redis 中用正则找key
获取 redis 中所有的 key 可用使用 *. redis 127.0.0.1:6379> KEYS * 1) "w3c3" 2) "w3c1" 3) ...
- python oracle使用心得
Oracel安装(windows 64位) 1. 首先确定版本. 2. 下载instantclient,下载地址:http://www.oracle.com/technetwork/database/ ...
- Hadoop生态圈-Azkaban部署实战
Hadoop生态圈-Azkaban部署实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Azkaban部署流程 1>.上传azkaban程序并创建解压目录 [yinz ...