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
gulab_days
31 gold badge1 silver badge4 bronze badges
1 Answer 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
Priyank Chheda
5815 silver badges18 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py