您应在此处或串联使用字符串格式化程序。对于串联,您必须将转换
int为
string。您不能将整数和字符串连接在一起。
如果您尝试这样做,将引发以下错误:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
格式:
quote = "You are %d years old" % agequote = "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())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)