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

博客主要围绕Python中使用%s格式化tuple类型字符串时出现的TypeError错误展开。指出可能原因是tuple不能用%s方式格式化字符串,给出的解决方法是使用str()方法将tuple转换为字符串进行格式化。

原文链接: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)
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值