0

What I am trying to do is x = "string1" y = "string2"

and output should be

string1 = "string2"

I have tried: exec("%s = %s" % (x,y))

If I put y =2 #or any number It will give the desired result but when I assign a string (y= "anystring" it tries to execute anystring and gives error: NameError( name "anystring" is not defined

Maybe I don't know exactly how to use the exec command. Any kind of help/suggestion would be helpful.

1 Answer 1

1

y value should be in quotes.

>>> x = "string1"
>>> y = "string2"
>>> exec("%s = '%s'" % (x,y))
>>> string1
'string2'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.