求问python中 %的用法?

求问python中 %的用法


不负相思意
浏览 770回答 2
2回答

大话西游666

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’

慕村9548890

最近遇到%用法的问题啦,在一句话中用了两次%s,两次字符串格式化,为什么运行后报错ValueError: I/O operation on closed file.程序好像默认%用作取余数的算法了。我运用的代码是报错是这样的实在是困惑不解啊?感觉写的也没有问题啊,为什么就是出错了呢?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python