• TypeError: unsupported operand type(s) for +: 'float' and 'str'


    说明:

    现在有float型值 5

    字符型值 a

    我原想它们组成一个这样的字符串:5a

    但是Python 不允许直接把数字和字符拼接在一起(如果拼在一起就会报标题显示的错误)

    示例:

    >>> 5+'a'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    >>> '5'+'a'
    '5a'

    解决办法:

    把数字型的字符串,转化为字符型就可以了

    >>> '5'+'a'
    '5a'
    >>> a=5
    >>> type(a)
    <class 'int'>
    >>> a = str(a)
    >>> type(a)
    <class 'str'>
    >>> a+'4'
    '54'
  • 相关阅读:
    mysql读写分离
    mysql主从同步
    扫描与抓包
    加密与入侵检查
    监控
    selinux
    预期交互
    python发送邮件
    linux下安装虚拟环境
    博弈论
  • 原文地址:https://www.cnblogs.com/kaerxifa/p/12882000.html
Copyright © 2020-2023  润新知