python元组格式化成字符串遇到typeError问题,pythontypeerror,>>> a=1>>> b
python元组格式化成字符串遇到typeError问题,pythontypeerror,>>> a=1>>> b
>>> a='1'>>> b='2','3'>>> print "%s,%s" % (a,b)1,('2', '3')>>> print "%s" % bTraceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: not all arguments converted during string formatting>>> print "%s" % (b)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: not all arguments converted during string formatting
如上所示,print "%s,%s" % (a,b)时能把元组b格式化成字符串,而在print "%s" % (b)中却出现了typeError却没有问题,请问是什么原因?
PS:用列表b进行两种操作都是成功的
>>>b>>>('2'.'3')>>>print "%s" % b相当于>>>print "%s" % ('2','3')
后面括号里的参数了呀~
print "%s" % (b,)
你要搞懂Python什么脾气。
要这么写
print "%s, %s" % b
% 操作符的第二个操作对象如果不是一个元组,那么它就只格式化一个值,否则它尝试把元组里每个值都单独格式化。你不可以同样是给它一个元组,有些时候要它当成多个值来格式化,另一些时候又把它当成单一的要格式化的值。
实在不习惯可以用 str 的 .format 方法代替。
另外,(b) 不是只包含一个元素 b 的元组,那个括号只是用于提升表达式的优先级,就像 (1 * 2) + 3 和 (2,) + 3 不一样类似。
编橙之家文章,
相关内容
- MAC下python2.7.8 IDLE运行返回效果的问题,python2.7.8idle,并
- 自己动手写python爬虫,参考什么学习资料适合,python爬虫
- transform: rotate样式失效了,transformrotate,DOM结构 <u
- 类似美元$这个符号pyCharm2.0里怎么输入不好用呢,美元
- python报错提示输出的疑问,python报输出疑问,类似这样的
- Python源码中爬虫没效果问题出在哪里,python爬虫,item.
- Python子线程停止方法问题,python线程停止,发现 threadi
- Python scrapy框架ImagesPipeline图片下载保存名称问题,,仔细
- python菜鸟求问关于嵌套函数中作用域范围应该怎么理解
- Python3.X中没sqlite3模块吗?为什么我导入时会提示没有模
评论关闭