Danny Bolland Danny Bolland - 1 year ago 78
Python Question

python variable concatination with different equators

How would I be able to make a variable that contains another = sign in it, like this:

newval = dict[key_to_find] = int(change)

Answer Source

The problem with doing this is that in Python the '=' is an assignment operator rather than a symbol.

Python is reading what you wrote as trying to assign to two variables. For example:

foo = "test" and bar = "test" can be written as foo = bar = "test"

Creating a variable name with an '=' it can't be done.

If you are trying to create a new variable that has a string value that comes from two other sources you can use the format that intboolstring suggested, or the format function is pretty handy.

Going along with your example:

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

To give a simpler example of how this works:

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

Variable will print:

"Hello world!"

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download