Python-如何连接str和int对象?

发布于 2021-02-02 23:20:35

如果我尝试执行以下操作:

things = 5
print("You have " + things + " things.")
我在Python 3.x中收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int

…以及Python 2.x中的类似错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

我该如何解决这个问题?

关注者
0
被浏览
217
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    这里的问题是+运算符在Python中(至少)具有两种不同的含义:对于数字类型,这意味着“将数字加在一起”:

    >>> 1 + 2
    3
    >>> 3.4 + 5.6
    9.0
    

    …,对于序列类型,它的意思是“连接序列”:

    >>> [1, 2, 3] + [4, 5, 6]
    [1, 2, 3, 4, 5, 6]
    >>> 'abc' + 'def'
    'abcdef'
    

    通常,Python不会为了使操作“有意义”而隐式地将对象从一种类型转换为另一种类型1,因为这会让人困惑:例如,你可能认为“3”+5应该表示“35”,但其他人可能认为它应该表示8,甚至“8”

    同样,Python不允许你连接两种不同类型的序列:

    >>> [7, 8, 9] + 'ghi'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can only concatenate list (not "str") to list
    

    因此,无论你要连接还是添加,都需要显式进行转换:

    >>> 'Total: ' + str(123)
    'Total: 123'
    >>> int('456') + 789
    1245
    

    但是,有更好的方法。根据所使用的Python版本,可以使用三种不同类型的字符串格式2,它们不仅使你能够避免多种+操作:

    >>> things = 5
    >>> 'You have %d things.' % things  # % interpolation
    'You have 5 things.'
    
    >>> 'You have {} things.'.format(things)  # str.format()
    'You have 5 things.'
    
    >>> f'You have {things} things.'  # f-string (since Python 3.6)
    'You have 5 things.'
    

    …但还可以控制值的显示方式:

    >>> value = 5
    >>> sq_root = value ** 0.5
    >>> sq_root
    2.23606797749979
    
    >>> 'The square root of %d is %.2f (roughly).' % (value, sq_root)
    'The square root of 5 is 2.24 (roughly).'
    
    >>> 'The square root of {v} is {sr:.2f} (roughly).'.format(v=value, sr=sq_root)
    'The square root of 5 is 2.24 (roughly).'
    
    >>> f'The square root of {value} is {sq_root:.2f} (roughly).'
    'The square root of 5 is 2.24 (roughly).'
    

    无论你使用%插值,str.format()还是f字符串取决于你:%插值的时间最长(并且对于使用C语言的人很熟悉),str.format()通常更强大,而f字符串仍然更强大(但仅在Python 3.6及更高版本中可用)。

    另一种选择是利用以下事实:如果你提供print多个位置参数,它将使用sep关键字参数(默认为’ ‘)将它们的字符串表示形式连接在一起:

    >>> things = 5
    >>> print('you have', things, 'things.')
    you have 5 things.
    >>> print('you have', things, 'things.', sep=' ... ')
    you have ... 5 ... things.
    

    …但这通常不如使用Python的内置字符串格式化功能灵活。

    1尽管它是数字类型的例外,但大多数人会同意要做的“正确”事情:

    >>> 1 + 2.3
    3.3
    >>> 4.5 + (5.6+7j)
    (10.1+7j)
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看