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 相连,表示浮点数类型保留小数点后两 ...
随机推荐
- 微信小程序input组件抖动及textarea组件光标错位解决方案
问题一: 使用微信小程序input组件时,在移动端唤起focus或blur事件时,因光标占位导致内容出现叠影及抖动现象. 解决方案: 用<textarea>组件代替了<input/& ...
- 转帖 IBM要推POWER9,来了解一下POWER处理器的前世今生
https://blog.csdn.net/kwame211/article/details/76669555 先来说一下最新的POWER 9 在Hot Chips会议上首次提到的IBM Power ...
- OneZero第二周第三次站立会议(2016.3.30)
会议时间:2016年3月30日 13:00~13:20 会议成员:冉华,张敏,王巍,夏一鸣. 会议目的:汇报前一天工作,全体成员评论,确定会后修改内容或分配下一步任务. 会议内容: 1.前端,完成功 ...
- react & monaco editor & vs code
react & monaco editor & vs code monaco-editor https://microsoft.github.io/monaco-editor/inde ...
- Dos命令大全完整版
DOS(磁盘操作系统)命令,是DOS操作系统的命令,是一种面向磁盘的操作命令,主要包括目录操作类命令.磁盘操作类命令.文件操作类命令和其它命令. 使用技巧 DOS命令不区分大小写,比如C盘的Progr ...
- Gym 100463A Crossings (树状数组 逆序对)
Crossings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description ...
- Firefox download 乱码问题。
import javax.mail.internet.MimeUtility; fileName = MimeUtility.encodeWord ( fileName ); response.add ...
- 【刷题】洛谷 P4320 道路相遇
题目描述 在 H 国的小 w 决定到从城市 \(u\) 到城市 \(v\) 旅行,但是此时小 c 由于各种原因不在城市 \(u\),但是小 c 决定到在中途与小 w 相遇 由于 H 国道路的原因,小 ...
- 【题解】 bzoj1503: [NOI2004]郁闷的出纳员 (Splay)
bzoj1503,懒得复制,戳我戳我 Solution: 我知不知道我是那根筋抽了突然来做splay,调了起码\(3h+\),到第二天才改出来(我好菜啊),当做训练调错吧 一个裸的splay,没啥好说 ...
- GDKOI2018发烧记
偏远小渔村NOIP螺旋升天选手又一次来到了广州参加GDKOI...金实的初三爷们也来啦?要被碾啦T T Day 0 跟HR Lao爷拼(biao)车到了高铁站,上了高铁居然没有颓颓颓吃吃吃(雾),安心 ...