Python通过format函数格式化显示值


Posted in Python onOctober 17, 2020

英文文档:

format(value[, format_spec])

Convert a value to a “formatted” representation, as controlled by format_spec. The interpretation of format_spec will depend on the type of the value argument, however there is a standard formatting syntax that is used by most built-in types: Format Specification Mini-Language.

The default format_spec is an empty string which usually gives the same effect as calling str(value).

A call to format(value, format_spec) is translated to type(value).__format__(value, format_spec) which bypasses the instance dictionary when searching for the value's __format__() method. A TypeError exception is raised if the method search reaches object and the format_spec is non-empty, or if either the format_spec or the return value are not strings.

格式化显示值

说明:

1. 函数功能将一个数值进行格式化显示。

2. 如果参数format_spec未提供,则和调用str(value)效果相同,转换成字符串格式化。

>>> format(3.1415936)
'3.1415936'
>>> str(3.1415926)
'3.1415926'

3. 对于不同的类型,参数format_spec可提供的值都不一样

#字符串可以提供的参数 's' None
>>> format('some string','s')
'some string'
>>> format('some string')
'some string'

#整形数值可以提供的参数有 'b' 'c' 'd' 'o' 'x' 'X' 'n' None
>>> format(3,'b') #转换成二进制
'11'
>>> format(97,'c') #转换unicode成字符
'a'
>>> format(11,'d') #转换成10进制
'11'
>>> format(11,'o') #转换成8进制
'13'
>>> format(11,'x') #转换成16进制 小写字母表示
'b'
>>> format(11,'X') #转换成16进制 大写字母表示
'B'
>>> format(11,'n') #和d一样
'11'
>>> format(11) #默认和d一样
'11'

#浮点数可以提供的参数有 'e' 'E' 'f' 'F' 'g' 'G' 'n' '%' None
>>> format(314159267,'e') #科学计数法,默认保留6位小数
'3.141593e+08'
>>> format(314159267,'0.2e') #科学计数法,指定保留2位小数
'3.14e+08'
>>> format(314159267,'0.2E') #科学计数法,指定保留2位小数,采用大写E表示
'3.14E+08'
>>> format(314159267,'f') #小数点计数法,默认保留6位小数
'314159267.000000'
>>> format(3.14159267000,'f') #小数点计数法,默认保留6位小数
'3.141593'
>>> format(3.14159267000,'0.8f') #小数点计数法,指定保留8位小数
'3.14159267'
>>> format(3.14159267000,'0.10f') #小数点计数法,指定保留10位小数
'3.1415926700'
>>> format(3.14e+1000000,'F') #小数点计数法,无穷大转换成大小字母
'INF'

#g的格式化比较特殊,假设p为格式中指定的保留小数位数,先尝试采用科学计数法格式化,得到幂指数exp,如果-4<=exp<p,则采用小数计数法,并保留p-1-exp位小数,否则按小数计数法计数,并按p-1保留小数位数
>>> format(0.00003141566,'.1g') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科学计数法计数,保留0位小数点
'3e-05'
>>> format(0.00003141566,'.2g') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科学计数法计数,保留1位小数点
'3.1e-05'
>>> format(0.00003141566,'.3g') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科学计数法计数,保留2位小数点
'3.14e-05'
>>> format(0.00003141566,'.3G') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科学计数法计数,保留0位小数点,E使用大写
'3.14E-05'
>>> format(3.1415926777,'.1g') #p=1,exp=0 ==》 -4<=exp<p成立,按小数计数法计数,保留0位小数点
'3'
>>> format(3.1415926777,'.2g') #p=1,exp=0 ==》 -4<=exp<p成立,按小数计数法计数,保留1位小数点
'3.1'
>>> format(3.1415926777,'.3g') #p=1,exp=0 ==》 -4<=exp<p成立,按小数计数法计数,保留2位小数点
'3.14'
>>> format(0.00003141566,'.1n') #和g相同
'3e-05'
>>> format(0.00003141566,'.3n') #和g相同
'3.14e-05'
>>> format(0.00003141566) #和g相同
'3.141566e-05'

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python实现拷贝多个文件到同一目录的方法
Sep 19 Python
CentOS 6.X系统下升级Python2.6到Python2.7 的方法
Oct 12 Python
使用python3构建文件传输的方法
Feb 13 Python
python处理DICOM并计算三维模型体积
Feb 26 Python
python3使用matplotlib绘制条形图
Mar 25 Python
python字符串中匹配数字的正则表达式
Jul 03 Python
对于Python深浅拷贝的理解
Jul 29 Python
Python对接六大主流数据库(只需三步)
Jul 31 Python
golang/python实现归并排序实例代码
Aug 30 Python
python 实现倒计时功能(gui界面)
Nov 11 Python
python如何调用php文件中的函数详解
Dec 29 Python
pandas apply使用多列计算生成新的列实现示例
Feb 24 Python
Python如何使用vars返回对象的属性列表
Oct 17 #Python
Python使用eval函数执行动态标表达式过程详解
Oct 17 #Python
Python基于locals返回作用域字典
Oct 17 #Python
Python classmethod装饰器原理及用法解析
Oct 17 #Python
Python基于staticmethod装饰器标示静态方法
Oct 17 #Python
详解python算法常用技巧与内置库
Oct 17 #Python
Python 操作SQLite数据库的示例
Oct 16 #Python
You might like
php 日期时间处理函数小结
2009/12/18 PHP
PHP+swoole+linux实现系统监控和性能优化操作示例
2019/04/15 PHP
javascript 兼容FF的onmouseenter和onmouseleave的代码
2008/07/19 Javascript
JavaScript Date对象 日期获取函数
2010/12/19 Javascript
为JS扩展Array.prototype.indexOf引发的问题探讨及解决
2013/04/24 Javascript
js简单实现用户注册信息的校验代码
2013/11/15 Javascript
JS使用replace()方法和正则表达式进行字符串的搜索与替换实例
2014/04/10 Javascript
浅谈javascript事件取消和阻止冒泡
2015/05/26 Javascript
自定义类似于jQuery UI Selectable 的Vue指令v-selectable
2017/08/23 jQuery
动态Axios的配置步骤详解
2018/01/12 Javascript
webpack-dev-server自动更新页面方法
2018/02/22 Javascript
JS实现马赛克图片效果完整示例
2019/04/13 Javascript
在Koa.js中实现文件上传的接口功能
2019/10/08 Javascript
javascript实现异形滚动轮播
2019/11/28 Javascript
文章或博客自动生成章节目录索引(支持三级)的实现代码
2020/05/10 Javascript
python使用MySQLdb访问mysql数据库的方法
2015/08/03 Python
详解Python中映射类型的内建函数和工厂函数
2015/08/19 Python
Python实现文件复制删除
2016/04/19 Python
Python中的日期时间处理详解
2016/11/17 Python
Python实现1-9数组形成的结果为100的所有运算式的示例
2017/11/03 Python
django之状态保持-使用redis存储session的例子
2019/07/28 Python
python isinstance函数用法详解
2020/02/13 Python
解决python 执行sql语句时所传参数含有单引号的问题
2020/06/06 Python
Python classmethod装饰器原理及用法解析
2020/10/17 Python
Python实例教程之检索输出月份日历表
2020/12/16 Python
CSS3实现自定义Checkbox特效实例代码
2017/04/24 HTML / CSS
用HTML5制作数字时钟的教程
2015/05/11 HTML / CSS
英国剑桥包中文官网:The Cambridge Satchel Company中国
2018/11/06 全球购物
英语专业毕业生自荐信
2013/10/28 职场文书
日化店促销方案
2014/03/26 职场文书
知识就是力量演讲稿
2014/09/13 职场文书
教师自我剖析材料(群众路线)
2014/09/29 职场文书
外贸英文求职信范文
2015/03/19 职场文书
大学生社会服务心得体会
2016/01/22 职场文书
详解Nginx 工作原理
2021/03/31 Servers
JS数组方法some、every和find的使用详情
2021/10/05 Javascript