Is there a way to create a global variable from a string? I know that you can make a variable from a string like so:
string = 'hello'
val = 10
vars()[string] = val
Thus making hello a variable equal to 10. I do not know how to make that user input variable global however, this does not work:
string = 'hello'
val = 10
vars()[string] = val
eval("global " + string)
You can use the globals()
function:
name = "hello"
globals()[name] = 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With