您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

将变量放入字符串(引号)

将变量放入字符串(引号)

您应在此处或串联使用字符串格式化程序。对于串联,您必须将转换intstring。您不能将整数和字符串连接在一起。

如果您尝试这样做,将引发以下错误

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())
其他 2022/1/1 18:37:05 有457人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶