Harshitha Palihawadana Harshitha Palihawadana - 1 year ago 82
Python Question

To convert string to variable name

I have any string. like 'buffalo',


I want to convert this string to some variable name like,


not only this example, I want to convert any input string to some variable name. How should I do that (in python)?

Answer Source
exec("%s = %d" % (x,2))

After that you can check it by:

print buffalo

As an output you will see: 2

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