0
>>a=input("Enter Variable Value: ")
123abc321zxc
>>b=input("Enter Variable Name: ")
xyz

Now i want xyz to be considered as variable name and 123abc321zxc as its value(value of 'a' to be assigned to value of b) so that i can get below result:

xyz="123abc321zxc"

NOTE: Use of database is not allowed. Its not a duplicate of any question. Please dont mark it duplicate

Please suggest

1 Answers1

1

To create a variable with a dynamic name you can do:

globals()['var_name'] = 'var_value'

This worked for me:

>>> globals()[input("Enter Variable Name: ")] = input("Enter Variable Value: ")
Enter Variable Value: 123
Enter Variable Name: aabbcc
>>> print(aabbcc)
123
>>>

EDIT:

Let me know if this is fine with you:

>>> g = globals() # Get the *current global symbol table*.
>>> var_name = input("Enter Variable Name: ")
Enter Variable Name: aabbcc
>>> var_value= input("Enter Variable Value: ")
Enter Variable Value: 123
>> g[var_name] = var_value # Add to the *current global symbol table* a name (named as the value of the variable *var_name*, 'aabbcc' in this case) and value with the value of the variable *var_value*.
>>> print(aabbcc)
123
>>>
  • Sorry. But here its just a=b. It doesnot fulfill my purpose. Its just like that i am creating variable name from UI as 'xyz' and i get the value after certain operation. Now i want that value to be stored in xyz variable name(which is given from UI) – Amit Kumar Aug 30 '17 at 18:17
  • Thanks gsi-frank. This helped me. :) – Amit Kumar Aug 30 '17 at 18:50