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 处理telnet返回的More,以及get想要的那个参数方法
Feb 14 Python
pandas.cut具体使用总结
Jun 24 Python
使用python serial 获取所有的串口名称的实例
Jul 02 Python
Python 计算任意两向量之间的夹角方法
Jul 05 Python
python Django里CSRF 对应策略详解
Aug 05 Python
对python中的os.getpid()和os.fork()函数详解
Aug 08 Python
详解Python中字符串前“b”,“r”,“u”,“f”的作用
Dec 18 Python
Python实现在Windows平台修改文件属性
Mar 05 Python
Django model.py表单设置默认值允许为空的操作
May 19 Python
python抢购软件/插件/脚本附完整源码
Mar 04 Python
python树莓派通过队列实现进程交互的程序分析
Jul 04 Python
Python+Matplotlib+LaTeX玩转数学公式
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静态类的原罪详解
2013/05/06 PHP
php比较相似字符串的方法
2015/06/05 PHP
php+ajax实现无刷新分页
2015/11/18 PHP
PHP实现的权重算法示例【可用于游戏根据权限来随机物品】
2019/02/15 PHP
PHP随机数函数rand()与mt_rand()的讲解
2019/03/25 PHP
php实现简单的守护进程创建、开启与关闭操作
2019/08/13 PHP
javascript 获取图片尺寸及放大图片
2013/09/04 Javascript
js操作输入框中选择内容兼容IE及其他主流浏览器
2014/04/22 Javascript
nodejs批量修改文件编码格式
2015/01/22 NodeJs
jQuery结合CSS制作漂亮的select下拉菜单
2015/05/03 Javascript
JavaScript每天必学之基础知识
2016/09/17 Javascript
JS+Canvas实现的俄罗斯方块游戏完整实例
2016/12/12 Javascript
karma+webpack搭建vue单元测试环境的方法示例
2018/05/24 Javascript
vue使用ElementUI时导航栏默认展开功能的实现
2018/07/04 Javascript
jquery实现的简单轮播图功能【适合新手】
2018/08/17 jQuery
详解Vue前端生产环境发布配置实战篇
2019/05/07 Javascript
jquery中为什么能用$操作
2019/06/18 jQuery
layui 根据后台数据动态创建下拉框并同时默认选中的实例
2019/09/02 Javascript
Element-UI+Vue模式使用总结
2020/01/02 Javascript
使用Vant完成Dialog弹框案例
2020/11/11 Javascript
[02:44]2014DOTA2 国际邀请赛中国区预选赛 大神红毯秀
2014/05/25 DOTA
python实现八大排序算法(1)
2017/09/14 Python
python爬虫_微信公众号推送信息爬取的实例
2017/10/23 Python
python实现机械分词之逆向最大匹配算法代码示例
2017/12/13 Python
解决Tensorflow占用GPU显存问题
2020/02/03 Python
Django 再谈一谈json序列化
2020/03/16 Python
Pygame框架实现飞机大战
2020/08/07 Python
使用bandit对目标python代码进行安全函数扫描的案例分析
2021/01/27 Python
《小小雨点》教学反思
2014/02/18 职场文书
后勤个人工作总结
2015/02/28 职场文书
小区物业管理2015年度工作总结
2015/10/22 职场文书
幼儿园2016年圣诞活动总结
2016/03/31 职场文书
《亲亲我的妈妈》观后感(3篇)
2019/09/26 职场文书
Mysql 如何查询时间段交集
2021/06/08 MySQL
Java并发编程之详解CyclicBarrier线程同步
2021/06/23 Java/Android
Redis分布式锁Redlock的实现
2021/08/07 Redis