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 相连,表示浮点数类型保留小数点后两 ...
随机推荐
- 组件 -- Badge
.badge :长方形的徽章 badge的颜色: .badge-primary .badge-secondary .badge-success .badge-warning ... ... .badg ...
- 课程回顾5in1
提出过的问题 问题1:敏捷开发在现阶段急于使用或试行,会不会得到相反的结果? 整个开发流程在施行了一整个学期,有积极的影响,也有消极的影响.例如通过这个流程的实施,规划短期的项目进度,使得成员能逐步了 ...
- LDAP的前世今生
上世界80年代,就有了LDAP的雏形. 我接触到最早的Windows系列的服务器,是Windows2000 Professional版本里可以加入ActiveDirectory,后来从Windows2 ...
- 转帖 OKR
什么是OKR OKR全称是Objectives and Key Results,即目标与关键成果法.OKR是一套定义和跟踪目标及其完成情况的管理工具和方法.1999年Intel公司发明了这种方法,后来 ...
- 解决sublime text3下中文无法输入的问题(Ubuntu)
sublime-text-imfix,非常无脑.就喜欢这样的.
- Git push -u orign master 提示hint: not have locally. This is usually caused by another repository push
一.情景 1.在GitHub上创建一个仓库A,并且初始化了readme.md这个文档. 2.在本地用Git Bash初始化仓库A(一开始没有从GitHub上拉下来). git init /* 初始化一 ...
- 【BZOJ1797】[AHOI2009]最小割(网络流)
[BZOJ1797][AHOI2009]最小割(网络流) 题面 BZOJ 洛谷 题解 最小割的判定问题,这里就当做记结论吧.(源自\(lun\)的课件) 我们先跑一遍最小割,求出残量网络.然后把所有还 ...
- XStream--java对象与xml形式文件相互转换
1.pom.xml中添加依赖 <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifa ...
- Pair_2测试与优化
# Pair_2测试与优化 211606316李震 21160305胡彤 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分 ...
- (转)每天一个linux命令(9):touch 命令
linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1 基本使用 1.命令格式: touch [选项]... 文件... 2.命令参数 ...