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易忽视知识点小结
May 25 Python
python实现发送和获取手机短信验证码
Jan 15 Python
Python基于dom操作xml数据的方法示例
May 12 Python
使用python将大量数据导出到Excel中的小技巧分享
Jun 14 Python
Python实现输入二叉树的先序和中序遍历,再输出后序遍历操作示例
Jul 27 Python
Python django框架应用中实现获取访问者ip地址示例
May 17 Python
python命令行参数用法实例分析
Jun 25 Python
Django框架模板用法入门教程
Nov 04 Python
Pytorch 多块GPU的使用详解
Dec 31 Python
python GUI库图形界面开发之PyQt5线程类QThread详细使用方法
Feb 26 Python
pycharm无法安装第三方库的问题及解决方法以scrapy为例(图解)
May 09 Python
python 实现表情识别
Nov 21 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之变量、常量学习笔记
2008/03/27 PHP
php中将时间差转换为字符串提示的实现代码
2011/08/08 PHP
PHP 安全检测代码片段(分享)
2013/07/05 PHP
php异步多线程swoole用法实例
2014/11/14 PHP
详解php比较操作符的安全问题
2015/12/03 PHP
配置Nginx+PHP的正确思路与过程
2016/05/10 PHP
php字符串操作针对负值的判断分析
2016/07/28 PHP
PHP实现上传图片到数据库并显示输出的方法
2018/05/31 PHP
PHP JWT初识及其简单示例
2018/10/10 PHP
CSS常用网站布局实例
2008/04/03 Javascript
div移动 输入框不能输入的问题
2009/11/19 Javascript
js判断IE浏览器版本过低示例代码
2013/11/22 Javascript
JavaScript中双叹号!!作用示例介绍
2014/09/21 Javascript
zepto.js中tap事件阻止冒泡的实现方法
2015/02/12 Javascript
Yii2使用Bootbox插件实现自定义弹窗
2015/04/02 Javascript
使用jQuery实现一个类似GridView的编辑,更新,取消和删除的功能
2017/03/15 Javascript
AngularJs 延时器、计时器实例代码
2017/09/16 Javascript
python连接远程ftp服务器并列出目录下文件的方法
2015/04/01 Python
简述:我为什么选择Python而不是Matlab和R语言
2017/11/14 Python
对Python 网络设备巡检脚本的实例讲解
2018/04/22 Python
详解Python数据可视化编程 - 词云生成并保存(jieba+WordCloud)
2019/03/26 Python
Django中的FBV和CBV用法详解
2019/09/15 Python
python3 BeautifulSoup模块使用字典的方法抓取a标签内的数据示例
2019/11/28 Python
linux 下python多线程递归复制文件夹及文件夹中的文件
2020/01/02 Python
python global和nonlocal用法解析
2020/02/03 Python
tensorflow使用指定gpu的方法
2020/02/04 Python
总监职责范文
2013/11/09 职场文书
心理健康教育心得体会
2013/12/29 职场文书
电脑租赁公司创业计划书
2014/01/08 职场文书
酒店值班经理的工作职责范本
2014/02/18 职场文书
创建精神文明单位实施方案
2014/03/08 职场文书
新店开张活动方案
2014/08/24 职场文书
纪念九一八事变演讲稿1000字
2014/09/14 职场文书
十二生肖观后感
2015/06/12 职场文书
Java 语言中Object 类和System 类详解
2021/07/07 Java/Android
用 Python 定义 Schema 并生成 Parquet 文件详情
2021/09/25 Python