Assigning input string as variable name
199 views (last 30 days)
Hi everyone, I have a quick question. I would like to ask the user to input the name of a variable and then use that string as a new variable name. For example, say I have the value [0 1 2 3 4]. I want to ask the user to give the name he would like to that variable.
So, for example:
x = input('Enter the name of the new variable: ') Say the user enters: new_var
new_var = [0 1 2 3 4]
Can someone explain how to do this?
Thanks for the help
Jan on 4 Jul 2012
This method will be insecure: Imagine the users selects a name, which is already used in your program. Then there is no way to avoid a crash reliably.
Another problem will be evil users, who type:
"system('format C:); y"
Then eval will assign y after it has crashed your computer. I admit, this will not be the standard case. But there are a lot of possible problems, which are less brute.
More Answers (3)
Arash Roozitalab on 18 Apr 2018
Hello. One might use
to assign a
to a string
var_name = "given_name"