python变量包含不同的等式

2024-04-20 16:21:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我怎样才能使一个变量包含另一个=符号,如下所示:

newval = dict[key_to_find] = int(change)

Tags: tokey符号findchangedictintnewval
2条回答

这样做的问题是,在Python中,“=”是赋值运算符,而不是符号。你知道吗

Python正在阅读您所写的内容,您试图将其分配给两个变量。例如:

foo = "test"bar = "test"可以写成foo = bar = "test"

无法创建带有“=”的变量名。

如果您试图创建一个新变量,该变量的字符串值来自其他两个源,那么可以使用intboolstring建议的格式,或者format函数非常方便。你知道吗

按照你的例子:

newval = "{dict} = {int}".format(dict=dict[key_to_find], int = int(change))

举一个简单的例子来说明它的工作原理:

var_a = 'Hello' var_b = 'world' variable = "{a} {b}!".format(a = var_a, b = var_b)

变量将打印:

"Hello world!"

我是根据

variable that contains another = sign in it

我想你试图在中间用一个等号来连接^ {< CD1>}和^ {CD2>}。这可以通过以下步骤完成

newval = str(dict[key_to_find]) + " = " + str(int(change))

我之所以在int强制转换中离开,是因为如果您将其更改为7.5,那么您会希望它作为7出现在字符串中。你知道吗

相关问题 更多 >