Python错误集锦:使用%s格式化tuple类型的字符串:TypeError: not all arguments converted during string formatting

原文链接:http://www.juzicode.com/archives/2731

错误提示:

使用%s格式化tuple类型的字符串时提示:

TypeError: not all arguments converted during string formatting

#juzicode.com/vx:桔子code
c = '桔子code'
d = [1,2,3,4,5]
e = (1,2,3,4,5)
print('c:%s'%(c))
print('d:%s'%(d))
print('e:%s'%(e))
c:桔子code
d:[1, 2, 3, 4, 5]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-33a548931ef0> in <module>
      5 print('c:%s'%(c))
      6 print('d:%s'%(d))
----> 7 print('e:%s'%(e))

TypeError: not all arguments converted during string formatting

可能原因:

1、tuple不能用%s方式格式化字符串。

 

解决方法:

1、使用str()方法格式化tuple转换为字符串:

#juzicode.com/vx:桔子code
c = '桔子code'
d = [1,2,3,4,5]
e = (1,2,3,4,5)
print('c:%s'%(c))
print('d:%s'%(d))
print('e:%s'%str(e))
c:桔子code
d:[1, 2, 3, 4, 5]
e:(1, 2, 3, 4, 5)
posted @ 2020-12-20 12:25  桔子code  阅读(105)  评论(0编辑  收藏  举报