Zer*_*eus 60 python string concatenation python-2.x python-3.x
如果我尝试执行以下操作:
things = 5
print("You have " + things + " things.")
Run Code Online (Sandbox Code Playgroud)
我在Python 3.x中收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
Run Code Online (Sandbox Code Playgroud)
......以及Python 2.x中的类似错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
Zer*_*eus 91
这里的问题是+操作符在Python中具有(至少)两种不同的含义:对于数字类型,它意味着"将数字加在一起":
>>> 1 + 2
3
>>> 3.4 + 5.6
9.0
Run Code Online (Sandbox Code Playgroud)
...对于序列类型,它意味着"连接序列":
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> 'abc' + 'def'
'abcdef'
Run Code Online (Sandbox Code Playgroud)
作为一项规则,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
Run Code Online (Sandbox Code Playgroud)
因此,您需要明确地进行转换,无论您想要的是串联还是添加:
>>> 'Total: ' + str(123)
'Total: 123'
>>> int('456') + 789
1245
Run Code Online (Sandbox Code Playgroud)
但是,有一种更好的方法.根据您使用的Python版本,有三种不同的字符串格式可用2,这不仅可以避免多个+操作:
>>> things = 5
Run Code Online (Sandbox Code Playgroud)
>>> 'You have %d things.' % things # % interpolation
'You have 5 things.'
Run Code Online (Sandbox Code Playgroud)
>>> 'You have {} things.'.format(things) # str.format()
'You have 5 things.'
Run Code Online (Sandbox Code Playgroud)
>>> f'You have {things} things.' # f-string (since Python 3.6)
'You have 5 things.'
Run Code Online (Sandbox Code Playgroud)
...还允许您控制值的显示方式:
>>> value = 5
>>> sq_root = value ** 0.5
>>> sq_root
2.23606797749979
Run Code Online (Sandbox Code Playgroud)
>>> 'The square root of %d is %.2f (roughly).' % (value, sq_root)
'The square root of 5 is 2.24 (roughly).'
Run Code Online (Sandbox Code Playgroud)
>>> 'The square root of {v} is {sr:.2f} (roughly).'.format(v=value, sr=sq_root)
'The square root of 5 is 2.24 (roughly).'
Run Code Online (Sandbox Code Playgroud)
>>> f'The square root of {value} is {sq_root:.2f} (roughly).'
'The square root of 5 is 2.24 (roughly).'
Run Code Online (Sandbox Code Playgroud)
无论你使用%插值,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.
Run Code Online (Sandbox Code Playgroud)
...但这通常不如使用Python的内置字符串格式化功能那么灵活.
1虽然它对数字类型有例外,但大多数人都同意"正确"的事情:
>>> 1 + 2.3
3.3
>>> 4.5 + (5.6+7j)
(10.1+7j)
Run Code Online (Sandbox Code Playgroud)
2实际上有四个......但模板字符串很少使用,有点尴尬.
TL; DR
要么:( print("You have " + str(things) + " things.")旧学校方式)
或:( print("You have {} things.".format(things))新的pythonic和推荐的方式)
更多口头解释:
虽然上面的优秀@Zero比雷埃夫斯答案中没有涉及任何内容,但我会尝试"缩小"它:
你不能在python中连接字符串和数字(任何类型),因为这些对象有不同的加号(+)运算符的定义彼此不兼容(在str的情况下+用于连接,在数字的情况下,它用于将两个数字加在一起).所以为了解决这个对象之间的"误解":
str(anything)方法将数字转换为字符串,
然后将结果与另一个字符串连接起来.玩得开心,并阅读@Zero Piraeus回答它肯定值得你花时间!