Assigning Variables Using String Variables
Consider a scenario where you have a variable assigned to a string and you wish to create a new variable based on that string. For instance:
foo = "bar" foo = "something else"
However, your true intention is to define a new variable using the string value, as follows:
bar = "something else"
To achieve this, the exec() function comes to your aid.
>>> foo = "bar" >>> exec(foo + " = 'something else'") >>> print bar something else >>>
In this example:
The above is the detailed content of How Can I Dynamically Create and Assign Variables Using String Variables in Python?. For more information, please follow other related articles on the PHP Chinese website!