Convert the contents of a string into name of variable

Fredrik Lundh fredrik at pythonware.com
Thu Mar 24 09:28:08 EST 2005


Erwan VITIERE wrote:

> I want to convert the contents of a string into name of variable.
> For example:
>
> var1="toto"
> ...
> toto=5
> print toto

why?

Python works better if you use it to write Python code.  the Python
solution is to use a dictionary:

    key1 = "toto"

    data = {}
    data["toto"] = 5

    print data[key1]

</F> 






More information about the Python-list mailing list