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实现strcmp函数功能示例
Mar 25 Python
python通过get,post方式发送http请求和接收http响应的方法
May 26 Python
详解Tensorflow数据读取有三种方式(next_batch)
Feb 01 Python
python Opencv将图片转为字符画
Feb 19 Python
Python3.4学习笔记之类型判断,异常处理,终止程序操作小结
Mar 01 Python
浅谈Python爬虫基本套路
Mar 25 Python
Python3中的最大整数和最大浮点数实例
Jul 09 Python
对python中的*args与**kwgs的含义与作用详解
Aug 28 Python
Python如何把Spark数据写入ElasticSearch
Apr 18 Python
详解pandas.DataFrame.plot() 画图函数
Jun 14 Python
Python爬虫之Selenium实现键盘事件
Dec 04 Python
手把手教你配置JupyterLab 环境的实现
Feb 02 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基于curl实现随机ip地址抓取内容的方法
2016/10/11 PHP
[原创]js与自动伸缩图片 自动缩小图片的多浏览器兼容的方法总结
2007/03/12 Javascript
javascript 异步页面查询实现代码(asp.net)
2010/05/26 Javascript
谈谈JavaScript中的函数与闭包
2013/04/14 Javascript
js document.write()使用介绍
2014/02/21 Javascript
jQuery操作元素css样式的三种方法
2014/06/04 Javascript
深入浅出分析javaScript中this用法
2015/05/09 Javascript
基于canvas实现的钟摆效果完整实例
2016/01/26 Javascript
node.js插件nodeclipse安装图文教程
2020/10/19 Javascript
关于网页中的无缝滚动的js代码
2016/06/09 Javascript
react开发教程之React 组件之间的通信方式
2017/08/12 Javascript
Vue-resource拦截器判断token失效跳转的实例
2017/10/27 Javascript
Angular resolve基础用法详解
2018/10/03 Javascript
Nodejs对postgresql基本操作的封装方法
2019/02/20 NodeJs
React Native登录之指纹登录篇的示例代码
2020/11/03 Javascript
vue+flask实现视频合成功能(拖拽上传)
2021/03/04 Vue.js
[55:25]2018DOTA2亚洲邀请赛3月29日 小组赛A组 VG VS OG
2018/03/30 DOTA
Python是编译运行的验证方法
2015/01/30 Python
在Python3中使用asyncio库进行快速数据抓取的教程
2015/04/02 Python
Python的Bottle框架中获取制定cookie的教程
2015/04/24 Python
Python 提取dict转换为xml/json/table并输出的实现代码
2016/08/28 Python
对numpy中array和asarray的区别详解
2018/04/17 Python
浅析Python函数式编程
2018/10/06 Python
python openCV获取人脸部分并存储功能
2019/08/28 Python
Python字符串大小写转换拼接删除空白
2019/09/19 Python
使用Keras预训练模型ResNet50进行图像分类方式
2020/05/23 Python
使用Python FastAPI构建Web服务的实现
2020/06/08 Python
Python实现验证码识别
2020/06/15 Python
墨尔本照明批发商店:Mica Lighting
2017/12/28 全球购物
如果有两个类A,B,怎么样才能使A在发生一个事件的时候通知B
2016/03/12 面试题
会计专业毕业生求职信分享
2014/01/03 职场文书
档案接收函范文
2014/01/10 职场文书
经典促销广告词大全
2014/03/19 职场文书
商场主管竞聘书
2014/03/31 职场文书
个人融资协议书范本两则
2014/10/15 职场文书
营销计划书范文
2015/01/17 职场文书