北京活动策划网站,新媒体营销策略有哪些,快盘做网站服务器,公司怎么做网络营销Python 程序设计入门#xff08;018#xff09;—— format() 函数的用法详解 目录 Python 程序设计入门#xff08;018#xff09;—— format() 函数的用法详解一、format() 函数的基本格式二、不提供 format_spec 参数三、设置字符串的对齐方式#xff08;align#x…Python 程序设计入门018—— format() 函数的用法详解 目录 Python 程序设计入门018—— format() 函数的用法详解一、format() 函数的基本格式二、不提供 format_spec 参数三、设置字符串的对齐方式align四、设置 sign 选项四、设置 \# 选项五、设置 grouping_option 选项六、设置 【0】和 width 选项七、设置 【.precision】选项八、设置 type 选项1、字符串类型2、整数类型3、浮点型 一、format() 函数的基本格式
format() 函数可以对数据进行格式化处理将值转换为由 format_spec 控制的【格式化】表示形式。format() 函数的语法格式如下
format(value, format_spec)说明
1value要转换的数据。
2format_spec格式化解释取决于值参数的类型
3默认情况下format_spec 是一个空字符串通常与调用 str(value) 函数具有相同的效果。
4format_spec 参数的格式包括填充值、文字对齐、标志设置、格式化、类型转换、千位符等应用。格式如下
format_spec ::[fill[align]] [sign] [z] [#] [0] [width] [grouping_option] [.precision] [type]
其中
fill :: any character
align :: | | | ^
sign :: | - |
width :: digit
grouping_option :: _ | ,
precision :: digit
type :: b | c | d | e | E | f | F | g | G | n | o | s | x | X二、不提供 format_spec 参数
如果不提供 format_spec 参数默认将其他格式数据转换为字符串类型和调用 str(value) 函数的效果相同。
例如
print(圆周率 format(3.14)) # 将浮点数转换为字符串
print(圆周率 str(3.14))运算结果为RESTART: C:\Python\Python38\First.py
圆周率3.14
圆周率3.14三、设置字符串的对齐方式align
使用如下选项设置字符串的对齐方式
符号含义‘’强制字段在有效空间内保持左对齐这是大多数对象的默认设置‘’强制字段在有效空间内保持右对齐这是大多数对象的默认设置‘’强制将填充字符放置在符号如果有之后、数字之前例如000000120。这种对齐选项仅对数字类型有效。当【0】紧接在字符宽度之前时它将成为填充数字时的默认值。‘^’强制字段在有效空间内保持居中对齐
例如
print(圆周率 format(3.14,10.2f) |end) # 默认为右对齐
print(圆周率 format(3.14,10.2f) |end) # 设置为右对齐
print(圆周率 format(3.14,10.2f) |end) # 设置为左对齐
print(圆周率 format(3.14,^10.2f) |end) # 设置为居中对齐
print(圆周率 format(3.14,A10.2f) |end) # 设置为右对齐前面填充A
print(圆周率 format(-3.14,A10.2f) |end) # 设置为右对齐前面填充A(A位于-符号之前)
print(圆周率 format(-3.14,A10.2f) |end) # 数字前面填充A(A位于-符号之后)运算结果为RESTART: C:\Python\Python38\First.py
圆周率 3.14|end
圆周率 3.14|end
圆周率3.14 |end
圆周率 3.14 |end
圆周率AAAAAA3.14|end
圆周率AAAAA-3.14|end
圆周率-AAAAA3.14|end四、设置 sign 选项
sign 选项只对数字类型有效sign 选项的含义如下
符号含义‘’表示正负数均可使用符号‘-’指示符号只被用于负数这是默认行为’ ’表示正数应使用前导空格负数使用负号
例如
print(圆周率 format(3.14,) |end) # 正数前面加号
print(圆周率 format(-3.14,) |end) # 负数前面加-号
print(圆周率 format(3.14,-) |end) # 正数前面不加号
print(圆周率 format(-3.14,-) |end) # 负数前面加号
print(圆周率 format(3.14, ) |end) # 正数前面加空格
print(圆周率 format(-3.14, ) |end) # 负数前面加-号运算结果为RESTART: C:\Python\Python38\First.py
圆周率3.14|end
圆周率-3.14|end
圆周率3.14|end
圆周率-3.14|end
圆周率 3.14|end
圆周率-3.14|end四、设置 # 选项
# 选项仅对整型、浮点型和负数型有效。# 选项的含义如下
符号含义## 选项仅对整型、浮点型和负数型有效。对于整数当使用二进制、八进制或十六进制输出时该选项会在输出值中添加相应的前缀0b、0o、0x 或0X。对于浮点型和负数型即使后面没有数字替换形式会导致转换结果始终包含小数点字符。对于 g 和 G 的科学计数法小数点的转换不会从结果中删除尾随零。
例如
print(以下为整型数据的输出)
print(format(11) |end) # 输出结果为十进制
print(format(11,#) |end) # 输出结果为十进制
print(format(11,b) |end) # 输出结果为二进制
print(format(11,#b) |end) # 输出结果为二进制输入0b
print(format(11,o) |end) # 输出结果为八进制
print(format(11,#o) |end) # 输出结果为八进制输入0o
print(format(11,x) |end) # 输出结果为十六进制
print(format(11,#x) |end) # 输出结果为十六进制输入0x
print(format(11,X) |end) # 输出结果为十六进制
print(format(11,#X) |end) # 输出结果为十六进制输入0X
print(以下为浮点型数据的输出)
print(format(11.8,10.0f) |end) # 无小数点
print(format(11.8,#10.0f) |end) # 有小数点
print(format(-11.8,#10.0F) |end) 运算结果为RESTART: C:\Python\Python38\First.py
以下为整型数据的输出
11|end
11|end
1011|end
0b1011|end
13|end
0o13|end
b|end
0xb|end
B|end
0XB|end
以下为浮点型数据的输出12|end12.|end-12.|end五、设置 grouping_option 选项
grouping_option 选项的含义如下
符号含义,表示使用逗号作为千位分隔符。_对浮点型和整型使用下划线作为千位分隔符。对于整型 b、o、x 和 x将每4位插入下划线。
例如
print(format(12345678) |end) # 不使用千位分隔符
print(format(12345678,,) |end) # 使用逗号作为千位分隔符
print(format(12345678.2541,,) |end) # 使用逗号作为千位分隔符
print(format(12345678.2541,_) |end) # 使用下划线作为千位分隔符
print(format(12345678,_b) |end) # 使用逗号作为千位分隔符运算结果为RESTART: C:\Python\Python38\First.py
12345678|end
12,345,678|end
12,345,678.2541|end
12_345_678.2541|end
1011_1100_0110_0001_0100_1110|end六、设置 【0】和 width 选项
[0] 和 width 选项的含义如下
符号含义[0] widthwidth定义字段总宽度包括任何前缀、分隔符和其他格式字符。如果未指定则字段宽度将由输出内容决定。如果未指定对齐方式则在宽度字段前面加一个零0相当于对齐类型 sign 为 的填充字符 0。
例如
print(format(3.14) |end) # 不指定宽度
print(format(3.14,10) |end) # 指定宽度为10默认右对齐
print(format(3.14,10) |end) # 指定宽度为10设置左对齐
print(format(3.14,010) |end) # 指定宽度为10左边用0填充运算结果为RESTART: C:\Python\Python38\First.py
3.14|end3.14|end
3.14 |end
0000003.14|end七、设置 【.precision】选项
[.precision] 选项的含义如下
符号含义[.precision]设置浮点数的精度是一个十进制整数。表示浮点类型 f 的小数点之后应显示多少位数字。表示科学计数法小数类型 g 的小数点前和后应显示多少位数。
例如
print(format(3.1415926) |end) # 不指定宽度
print(format(3.1415926,10.1f) |end) # 指定宽度为10小数位数1
print(format(3.1415926,10.2f) |end) # 指定宽度为10小数位数2
print(format(3.1415926,.1f) |end) # 不指定宽度小数位数1
print(format(3.1415926,.2f) |end) # 不指定宽度小数位数2运算结果为RESTART: C:\Python\Python38\First.py
3.1415926|end3.1|end3.14|end
3.1|end
3.14|end八、设置 type 选项
type 选项针对不同的数据类型有如下格式
1、字符串类型
type 选项及含义如下
符号含义s字符串格式。None和 s 一样。
例如
print(format(Chinese,010) |end) # 左对齐右侧用0填充
print(format(Chinese,010) |end) # 右对齐左侧用0填充
print(format(Chinese,0^10) |end) # 居中对齐左右两侧用0填充
print(format(Chinese,10.3) |end) # 左对齐截取左端3个字符
print(format(Chinese,10.3) |end) # 右对齐截取左端3个字符
print(format(Chinese,^10.3) |end) # 居中对齐截取左端3个字符运算结果为RESTART: C:\Python\Python38\First.py
Chinese000|end
000Chinese|end
0Chinese00|end
Chi |endChi|endChi |end2、整数类型
type 选项及含义如下
符号含义b二进制格式c将字符在打印之前将整数转换为相应的 unicode 字符d十进制整数o八进制格式x十六进制格式使用小写字母表示 9 以上的数码X十六进制格式使用大写字母表示 9 以上的数码n与 d 相似不同之处在于它会使用当前区域设置来插入适当的数字分隔字符
例如
print(format(1234567,b) |end) # 二进制
print(format(1234567,o) |end) # 八进制
print(format(1234567,d) |end) # 十进制
print(format(1234567,n) |end) # 十进制
print(format(1234567,x) |end) # 十六进制
print(format(1234567,X) |end) # 十六进制
print(format(4567,c) |end) # 打印Unicode字符运算结果为RESTART: C:\Python\Python38\First.py
100101101011010000111|end
4553207|end
1234567|end
1234567|end
12d687|end
12D687|end
ᇗ|end3、浮点型
type 选项及含义如下
符号含义e科学计数法。对于一个给定的精度 p将数字格式化为以字母 e 分隔系数和指数的科学计数法形式。 系数在小数点之前有一位之后有 p 位总计 p 1 个有效数位。 如未指定精度则会对 float 采用小数点之后 6 位精度而对 Decimal 则显示所有系数位。如果小数点之后没有数位则小数点也会被略去除非使用了 # 选项。E科学计数法。 与 e 相似不同之处在于它使用大写字母 E 作为分隔字符。f定点表示法。对于一个给定的精度 p将数字格式化为在小数点之后恰好有 p 位的小数形式。 如未指定精度则会对 float 采用小数点之后 6 位精度而对 Decimal 则使用大到足够显示所有系数位的精度。如果小数点之后没有数位则小数点也会被略去除非使用了# 选项。g1常规格式。 对于给定精度 p 1这会将数值舍入到 p 个有效数位再将结果以定点表示法或科学计数法进行格式化具体取决于其值的大小。 精度 0 会被视为等价于精度1。2如未指定精度会对 float 采用 6 个有效数位的精度。 对于 Decimal结果的系数会沿用原值的系数数位对于绝对值小于 1e-6 的值以及最小有效数位的位值大于 1 的数值将会使用科学计数法在其他情况下则会使用定点表示法即科学计数法。%百分比。 将数字乘以 100 并显示为定点f格式后面带一个百分号。
例如
print(以下为科学计数法表示)
print(format(1234567.123,e) |end) # 6位小数
print(format(1234567.123,15.10e) |end)# 10位小数
print(format(1234567.123,15.10E) |end)# 10位小数字母E大写
print(format(1234567.123,15.0e) |end) # 0位小数
print(format(1234567.123,#15.0e) |end)# 0位小数
print(以下为定点数表示)
print(format(1234567.123,#15.10f) |end)# 10位小数
print(format(1234567.123,15.0f) |end) # 0位小数
print(format(1234567.123,#15.0f) |end) # 0位小数
print(format(0.123,.2%) |end) # 百分比运算结果为RESTART: C:\Python\Python38\First.py
以下为科学计数法表示
1.234567e06|end
1.2345671230e06|end
1.2345671230E06|end1e06|end1.e06|end
以下为定点数表示
1234567.1229999999|end1234567|end1234567.|end
12.30%|end
文章转载自: http://www.morning.wpqwk.cn.gov.cn.wpqwk.cn http://www.morning.hyyxsc.cn.gov.cn.hyyxsc.cn http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.yhsrp.cn.gov.cn.yhsrp.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.wgzzj.cn.gov.cn.wgzzj.cn http://www.morning.ddtdy.cn.gov.cn.ddtdy.cn http://www.morning.rfwrn.cn.gov.cn.rfwrn.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.xdmsq.cn.gov.cn.xdmsq.cn http://www.morning.sbdqy.cn.gov.cn.sbdqy.cn http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.ltpph.cn.gov.cn.ltpph.cn http://www.morning.qfdyt.cn.gov.cn.qfdyt.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.hytqt.cn.gov.cn.hytqt.cn http://www.morning.ldqzz.cn.gov.cn.ldqzz.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.lptjt.cn.gov.cn.lptjt.cn http://www.morning.wxqmc.cn.gov.cn.wxqmc.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.lbrrn.cn.gov.cn.lbrrn.cn http://www.morning.zzqgc.cn.gov.cn.zzqgc.cn http://www.morning.bpmdg.cn.gov.cn.bpmdg.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.ryzgp.cn.gov.cn.ryzgp.cn http://www.morning.mzcrs.cn.gov.cn.mzcrs.cn http://www.morning.thbnt.cn.gov.cn.thbnt.cn http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.hfytgp.cn.gov.cn.hfytgp.cn http://www.morning.hjlwt.cn.gov.cn.hjlwt.cn http://www.morning.crtgd.cn.gov.cn.crtgd.cn http://www.morning.qxlhj.cn.gov.cn.qxlhj.cn http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn http://www.morning.wdpbq.cn.gov.cn.wdpbq.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.rkzk.cn.gov.cn.rkzk.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.bryyb.cn.gov.cn.bryyb.cn http://www.morning.nywrm.cn.gov.cn.nywrm.cn http://www.morning.hydkd.cn.gov.cn.hydkd.cn http://www.morning.mdnnz.cn.gov.cn.mdnnz.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.lgsfb.cn.gov.cn.lgsfb.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.rsszk.cn.gov.cn.rsszk.cn http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.pphbn.cn.gov.cn.pphbn.cn http://www.morning.gydsg.cn.gov.cn.gydsg.cn http://www.morning.jnoegg.com.gov.cn.jnoegg.com http://www.morning.sryhp.cn.gov.cn.sryhp.cn http://www.morning.yjknk.cn.gov.cn.yjknk.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.gsyns.cn.gov.cn.gsyns.cn http://www.morning.qynnw.cn.gov.cn.qynnw.cn http://www.morning.gjlst.cn.gov.cn.gjlst.cn http://www.morning.drbwh.cn.gov.cn.drbwh.cn http://www.morning.wcqkp.cn.gov.cn.wcqkp.cn http://www.morning.csjps.cn.gov.cn.csjps.cn http://www.morning.glncb.cn.gov.cn.glncb.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.hbnwr.cn.gov.cn.hbnwr.cn http://www.morning.rfwkn.cn.gov.cn.rfwkn.cn http://www.morning.krdmn.cn.gov.cn.krdmn.cn http://www.morning.wdxr.cn.gov.cn.wdxr.cn http://www.morning.crrjg.cn.gov.cn.crrjg.cn http://www.morning.skdhm.cn.gov.cn.skdhm.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn http://www.morning.llqky.cn.gov.cn.llqky.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.rkwwy.cn.gov.cn.rkwwy.cn http://www.morning.dskmq.cn.gov.cn.dskmq.cn http://www.morning.qhczg.cn.gov.cn.qhczg.cn