您应在此处或串联使用字符串格式化程序。对于串联,您必须将转换int
为string
。您不能将整数和字符串连接在一起。
如果您尝试这样做,将引发以下错误:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
格式:
quote = "You are %d years old" % age
quote = "You are {} years old".format(age)
串联(单向)
quote = "You are " + str(age) + " years old"
:正如JF Sebastian在评论中指出的,我们还可以执行以下操作
在Python 3.6中:
f"You are {age} years old"
Python的早期版本:
"You are {age} years old".format(**vars())