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 示例分享---逻辑推理编程解决八皇后
Jul 20 Python
Python中类的继承代码实例
Oct 28 Python
python中使用序列的方法
Aug 03 Python
Python基于递归算法实现的汉诺塔与Fibonacci数列示例
Apr 18 Python
Python实现的建造者模式示例
Aug 06 Python
详解Python 装饰器执行顺序迷思
Aug 08 Python
解决导入django_filters不成功问题No module named 'django_filter'
Jul 15 Python
如何快速一次性卸载所有python包(第三方库)呢
Oct 20 Python
python中pop()函数的语法与实例
Dec 01 Python
python 如何上传包到pypi
Dec 24 Python
五分钟学会怎么用python做一个简单的贪吃蛇
Jan 12 Python
python绘制汉诺塔
Mar 01 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 之 写时复制介绍(Copy On Write)
2014/05/13 PHP
destoon数据库表说明汇总
2014/07/15 PHP
叫你如何修改Nginx与PHP的文件上传大小限制
2014/09/10 PHP
PHP冒泡算法详解(递归实现)
2014/11/10 PHP
php上传文件并显示上传进度的方法
2015/03/24 PHP
PHP生成json和xml类型接口数据格式
2015/05/17 PHP
Laravel框架基础语法与知识点整理【模板变量、输出、include引入子视图等】
2019/12/03 PHP
将HTMLCollection/NodeList/伪数组转换成数组的实现方法
2011/06/20 Javascript
javascript中createElement的两种创建方式
2015/05/14 Javascript
纯原生js实现table表格的增删
2017/01/05 Javascript
vue实现简单表格组件实例详解
2017/04/16 Javascript
vue按需引入element Transfer 穿梭框
2017/09/30 Javascript
微信小程序异步处理详解
2017/11/10 Javascript
node错误处理与日志记录的实现
2018/12/24 Javascript
JavaScript中变量提升机制示例详解
2019/12/27 Javascript
vue实现自定义多选按钮
2020/07/16 Javascript
[01:46]DOTA2上海特锦赛小组赛英文解说KotlGuy采访
2016/02/27 DOTA
[01:03:00]DOTA2上海特级锦标赛A组败者赛 EHOME VS CDEC第一局
2016/02/25 DOTA
[33:42]LGD vs OG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
Python中使用glob和rmtree删除目录子目录及所有文件的例子
2014/11/21 Python
python实现多线程暴力破解登陆路由器功能代码分享
2015/01/04 Python
python开发之for循环操作实例详解
2015/11/12 Python
详解Python中的变量及其命名和打印
2016/03/11 Python
Python sys模块常用方法解析
2020/02/20 Python
python pip如何手动安装二进制包
2020/09/30 Python
matplotlib之pyplot模块之标题(title()和suptitle())
2021/02/22 Python
英国独特礼物想法和个性化礼物网站:notonthehighstreet.com
2018/04/16 全球购物
黄色火烈鸟:De Gele Flamingo
2019/03/18 全球购物
什么是继承
2013/12/07 面试题
优秀企业获奖感言
2014/02/01 职场文书
《长城》教学反思
2014/02/14 职场文书
服务之星事迹材料
2014/05/03 职场文书
态度决定一切演讲稿
2014/05/20 职场文书
环境监测与治理技术专业求职信
2014/07/06 职场文书
2015年中学元旦晚会活动方案
2014/12/09 职场文书
Vue Mint UI mt-swipe的使用方式
2022/06/05 Vue.js