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

2

1 Answer 1

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
>>>
Sign up to request clarification or add additional context in comments.

2 Comments

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)
Thanks gsi-frank. This helped me. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.