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中的zip函数使用示例
Jan 29 Python
Python实现将Excel转换为json的方法示例
Aug 05 Python
Python+PIL实现支付宝AR红包
Feb 09 Python
Python 对输入的数字进行排序的方法
Jun 23 Python
python 并发编程 阻塞IO模型原理解析
Aug 20 Python
Python中的相关分析correlation analysis的实现
Aug 29 Python
tensorflow mnist 数据加载实现并画图效果
Feb 05 Python
django model 条件过滤 queryset.filter(**condtions)用法详解
May 20 Python
Python自动化xpath实现自动抢票抢货
Sep 19 Python
如何通过安装HomeBrew来安装Python3
Dec 23 Python
python opencv人脸识别考勤系统的完整源码
Apr 26 Python
python 解决微分方程的操作(数值解法)
May 26 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
《星际争霸II》全新指挥官斯台特曼现已上线
2020/03/08 星际争霸
PHP语法速查表
2006/12/06 PHP
Windows2003下php5.4安装配置教程(Apache2.4)
2016/06/30 PHP
使用ThinkPHP的自动完成实现无限级分类实例详解
2016/09/02 PHP
利用PHP实现一个简单的用户登记表示例
2017/04/25 PHP
JS定时器实例
2013/04/17 Javascript
jquery之超简单的div显示和隐藏特效demo(分享)
2013/07/09 Javascript
jQuery toggleClass应用实例(附效果图)
2014/04/06 Javascript
js识别不同浏览器基于userAgent做判断
2014/07/29 Javascript
深入理解javascript构造函数和原型对象
2014/09/23 Javascript
解决angular的$http.post()提交数据时后台接收不到参数值问题的方法
2015/12/10 Javascript
简单的JS时钟实例讲解
2016/01/13 Javascript
实例讲解Jquery中隐藏hide、显示show、切换toggle的用法
2016/05/13 Javascript
必备的JS调试技巧汇总
2016/07/20 Javascript
深入分析node.js的异步API和其局限性
2016/09/05 Javascript
手机端实现Bootstrap简单图片轮播效果
2016/10/13 Javascript
原生JavaScript实现精美的淘宝轮播图效果示例【附demo源码下载】
2017/05/27 Javascript
node+vue实现用户注册和头像上传的实例代码
2017/07/20 Javascript
Vue列表页渲染优化详解
2017/07/24 Javascript
浅谈Vue SSR 的 Cookies 问题
2017/11/20 Javascript
解决vue打包后刷新页面报错:Unexpected token
2019/08/27 Javascript
[50:29]2014 DOTA2华西杯精英邀请赛 5 24 DK VS iG
2014/05/26 DOTA
[01:01]青春无憾,一战成名——DOTA2全国高校联赛开启
2018/02/25 DOTA
关于反爬虫的一些简单总结
2017/12/13 Python
对numpy和pandas中数组的合并和拆分详解
2018/04/11 Python
pyttsx3实现中文文字转语音的方法
2018/12/24 Python
Pytorch保存模型用于测试和用于继续训练的区别详解
2020/01/10 Python
Talbots官网:美国成熟女装品牌
2019/11/15 全球购物
大专毕业生自我鉴定
2013/11/21 职场文书
红旗方阵解说词
2014/02/12 职场文书
学生社团文化节开幕式主持词
2014/03/28 职场文书
《乡下孩子》教学反思
2014/04/17 职场文书
护士节活动总结
2014/08/29 职场文书
四风批评与自我批评发言稿
2014/10/14 职场文书
护理心得体会范文
2016/01/22 职场文书
mysql如何查询连续记录
2022/05/11 MySQL