zoukankan      html  css  js  c++  java
  • 字符串、列表、元组 中文输出问题

    >>> 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'> #说明('美国',)才是元组

  • 相关阅读:
    JS 实现鼠标移入移出透明度动画变化效果
    Undefined和null的本质区别
    网格布局知识点总结
    用CSS3搭建立方体
    缩放实例
    浮动与细线边框制作广告商标
    用伪元素制作列表菜单
    元素的分类与转换
    网易云导航栏
    CSS中内边距和宽度内减
  • 原文地址:https://www.cnblogs.com/huangbiquan/p/7812045.html
Copyright © 2011-2022 走看看