Convert string to variable name in python [duplicate]

x='buffalo'    
exec("%s = %d" % (x,2))

After that you can check it by:

print buffalo

As an output you will see:
2

Leave a Comment