What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
For a name chosen at runtime, store the value in a dictionary: use the string as a key, then look up or update that key. A string such as "score" does not automatically become a Python variable name. Use globals() only when you deliberately need to bind a module-level name; avoid exec() for ordinary data storage.
Why a string does not automatically become a variable
In Python, a variable name is a name bound to an object. A string that contains the same characters is still just a string until your code uses it as a namespace key or executes code that refers to that name. For example, name = "score" creates the variable name; it does not create a variable called score.
As an Amazon Associate I earn from qualifying purchases.
When the set of names is selected while a program runs, represent those names as data rather than trying to create identifiers dynamically. The Python programming FAQ and the execution model documentation explain Python’s name-binding and scope rules.
Recommended Free Tools
Use a dictionary for runtime-selected names
A dictionary is the clearest way to associate arbitrary string keys with values. The key can come from a variable, a file, an API response, or user input.
#1 Best Overall
name = "score"
values = {}
values[name] = 42
print(values["score"]) # 42
To update or retrieve an entry later, use the same key:
values[name] = 50
print(values[name]) # 50
This approach keeps the data in an explicit container. It also avoids changing the program’s namespace just to hold a value whose label happens to be text.
Rank #2
When a real module-level name is necessary
If another part of a module specifically needs a binding in that module’s global namespace, globals() returns a mapping of the global names. A key in that mapping can be assigned a value:
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallname = "score"
globals()[name] = 42
print(score) # 42, in this module's global namespace
Use this narrowly. Dynamically adding module globals can make code harder to understand and inspect; for values selected at runtime, a dictionary is usually clearer.
Why this does not create a function-local variable
globals() refers to the module namespace, not a function’s ordinary local namespace. Python determines local names from binding operations in a function body. Assigning to a name there makes it local unless the code declares it global or, for a binding in an enclosing function, nonlocal. Reading a local name before it has been assigned can raise UnboundLocalError.
For function-local state whose keys are chosen at runtime, use a local dictionary:
def make_values(name, value):
values = {}
values[name] = value
return values
result = make_values("score", 42)
print(result["score"]) # 42
See the Python execution model for the language’s rules about local and global names.
Why not use exec()?
exec() executes Python code supplied as text; it is not a conversion from a string to a variable. Building an assignment statement from a string adds scope complications and can execute unintended code if the text is untrusted. The Python FAQ warns about security risks from dynamic evaluation when a simpler approach will do, and the language reference’s dynamic-features section documents code-execution facilities.
Best Value
If a name comes from user input, keep it as a dictionary key rather than inserting it into executable Python source. A mapping treats the input as data; code execution treats it as instructions.
Quick Recap
Choose the right approach
| Approach | What it does | Best fit |
|---|---|---|
Dictionary, such as values[name] |
Stores a value under a string key | Runtime-selected names and dynamic collections |
globals()[name] |
Binds a name in the module’s global namespace | A genuine need for a module-level binding |
exec() |
Executes Python code from text | Not ordinary dynamic storage; avoid for this purpose |
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




