>>> print('%s%' %'1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: incomplete format
>>> print('num is d%' % num)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'num' is not defined
>>> print('num is d%%' % 5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>> print('num is d%' % 5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: incomplete format
>>> print('num is d%' % 5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: incomplete format
>>> print('%%[%s,%.2f]%%' %('1',0.986))
%[1,0.99]%
>>> print('[%s,%.2f]' %('1',0.986))
[1,0.99]
>>> print('[%d,%.2f]' %(1,0.986))
[1,0.99]
>>> print('[%d]' %(1))
[1]
>>> print('%d' %(1))
1
>>> print('%d' % 1)
1
总结一下: %d 写成 d%,百分号在后面就会出现这种错误。
>>> print('num is d%' % 5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: incomplete format
而%写在前面成%d就是正确的
>>> print('%d' % 1)
1