为了账号安全,请及时绑定邮箱和手机立即绑定

求问python中 %的用法?

求问python中 %的用法?

不负相思意 2019-02-07 10:11:15
求问python中 %的用法
查看完整描述

2 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

0.2f中"."前面的数字表示这个数字要占有多少个字节,“0”表示不刻意规定占用字节数,只要能表示这个数字就行。而“.”后面的数字表示小数位数。
d表示这个数据类型是整型,还有s表示数据类型是字符串。
以下是我们python教材中的原话,供你参考:
>>> "Hello %s %s, you may have won $%d!" % ("Mr.", "Smith", 10000)
’Hello Mr. Smith, you may have already won $10000!’
>>> ’This int, %5d, was placed in a field of width 5’ % (7)
’This int, 7, was placed in a field of width 5’
>>> ’This int, %10d, was placed in a field of width 10’ % (7)
’This int, 7, was placed in a field of width 10’
>>> ’This float, %10.5f, has width 10 and precision 5.’ % (3.1415926)
’This float, 3.14159, has width 10 and precision 5.’
>>> ’This float, %0.5f, has width 0 and precision 5.’ % (3.1415926)
’This float, 3.14159, has width 0 and precision 5.’
>>> "Compare %f and %0.20f" % (3.14, 3.14)
’Compare 3.140000 and 3.14000000000000012434’

查看完整回答
反对 回复 2019-03-11
?
慕村9548890

TA贡献1884条经验 获得超4个赞

最近遇到%用法的问题啦,在一句话中用了两次%s,两次字符串格式化,为什么运行后报错ValueError: I/O operation on closed file.程序好像默认%用作取余数的算法了。

我运用的代码是

报错是这样的

实在是困惑不解啊?感觉写的也没有问题啊,为什么就是出错了呢?


查看完整回答
反对 回复 2019-03-11
  • 2 回答
  • 0 关注
  • 768 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信