字符串、列表、元组 中文输出问题

>>> tmp = ['中国','英国']
>>> tmp = tmp[:1] + ['美国'] + tmp[1:]
>>> tmp = tmp[:1] + ['德国'] + tmp[1:]
>>> tmp
['中国', '德国', '美国', '英国']
>>> tmp = ['中国','英国']
>>> tmp = tmp[:1] + ['美国'] + tmp[1:]
>>> tmp
['中国', '美国', '英国']
>>> tmp = tmp[:1] + ['德国',] + tmp[1:]
>>> tmp2 = ('中国','英国')
>>> tmp2 = tmp2[:1] + ('美国') + tmp2[1:]
Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
tmp2 = tmp2[:1] + ('美国') + tmp2[1:]
TypeError: can only concatenate tuple (not "str") to tuple #只能元组和元组连接(相加)
>>> tmp2 = tmp2[:1] + ('美国,') + tmp2[1:]
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
tmp2 = tmp2[:1] + ('美国,') + tmp2[1:]
TypeError: can only concatenate tuple (not "str") to tuple
>>> tmp2 = tmp2[:1] + ('美国',) + tmp2[1:]
>>> tmp2
('中国', '美国', '英国')

>>> s1 = ('美国')
>>> s2 = ('美国,')
>>> s3 = ('美国',)
>>> type(s1)
<class 'str'> #说明('美国')是一个字符串,而不是元组
>>> type(s2)
<class 'str'> #说明('美国,')是一个字符串,而不是元组
>>> type(s3)
<class 'tuple'> #说明('美国',)才是元组

posted @ 2017-11-09 23:38  碧水幽幽泉  阅读(1196)  评论(0编辑  收藏  举报