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.

asked Nov 10, 2020 at 12:30

1 Answer 1

1

y value should be in quotes.

>>> x = "string1"
>>> y = "string2"
>>> exec("%s = '%s'" % (x,y))
>>> string1
'string2'
answered Nov 10, 2020 at 12:36
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.