英文文档:

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内置函数(46)——format的更多相关文章

  1. Python内置函数(46)——oct

    英文文档: oct(x) Convert an integer number to an octal string. The result is a valid Python expression. ...

  2. Python内置函数(23)——format

    英文文档: format(value[, format_spec]) Convert a value to a “formatted” representation, as controlled by ...

  3. Python内置函数之format()

    format(value[,format_spec])返回字符串对象. 可以用来格式化value. >>> format(,'0.3f') #保留3位小数 '12.000' > ...

  4. Python之路(第八篇)Python内置函数、zip()、max()、min()

    一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...

  5. Python之路Python内置函数、zip()、max()、min()

    Python之路Python内置函数.zip().max().min() 一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算, ...

  6. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  7. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  8. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  9. python 内置函数和函数装饰器

    python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...

随机推荐

  1. Android学习之AutoCompleteTextView和MultiAutoCompleteTextView

    转自:http://blog.csdn.net/qq_28468727/article/details/52258409 AutoCompleteTextView.MultiAutoCompleteT ...

  2. c标准头文件

    好多C语言库函数参考还是用的TC的库函数参考,因此特地把现在C语言(C99)标准库函数的24个头文件列表如下:assert.h types.h(C99)  signal.h  stdlib.h   c ...

  3. 设计模式——备忘录模式(C++实现)

    #include <iostream> #include <string> #include <vector> using namespace std; class ...

  4. 在Jenkins中配置执行远程shell命令

    1.想要 远程登录到linux服务器并执行相应的shell脚本,需要在jenkins上安装插件enkins SSH plugin 2. 安装了这个插件后,进入系统的配置管理中配置 SSH remote ...

  5. Django数据模型——通用字段选项

    字段 一个模型最重要也是唯一必需的部分,是它定义的数据库字段 字段名称限制 1.字段名不能是python保留字,这样会导致python语法错误 2.字段不能包含连续一个以上的下划线,这样会和Djang ...

  6. ES6之promise的使用

    let checkLogin = function () { return new Promise(function (resolve,reject) { let flag = document.co ...

  7. Objective-C 中的 BOOL

    之前开发了一个针对单个数据模型,自动建表.增删改查等操作的 ORM 库,后边在 iPhone 5c 上使用时,出现了 crash 的情况. 该项目在 Github 开源,项目地址为SXJDatabas ...

  8. Flyway--数据库版本管理和控制工具

    1. Flyway 的主要任务是管理数据库的版本更新,在Flyway 中称每次数据库更新为一个migration ,为了更顺口,我们下面称之为数据库脚本.Flyway 支持SQL-based migr ...

  9. Day3--------------目录文件的浏览、管理及维护

    1.pwd命令功能 2.cd命令功能 cd..      返回上一级 cd~ cd- 返回上一次的目录 cd 3.ls命令功能 ls -a ls -all ls -l 4.cp命令功能 cp -i 覆 ...

  10. 把文件每隔三行合并成一行(awk之RS、ORS与FS、OFS)

    比如文本如下:123abc合并后的结果是:1 2 3a b c #.txt a b c awk之RS.ORS与FS.OFS 转自http://www.cnblogs.com/fhefh/archive ...