If you really want to create them on the fly you can assign to the dict that is returned by either globals() or locals() depending on what namespace you want to create them in:

globals()['somevar'] = 'someval'
print somevar  # prints 'someval'

But I wouldn't recommend doing that. In general, avoid global variables. Using locals() often just obscures what you are really doing. Instead, create your own dict and assign to it.

mydict = {}
mydict['somevar'] = 'someval'
print mydict['somevar']

Learn the python zen; run this and grok it well:

>>> import this
Answer from kanaka on Stack Overflow
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Dynamic variable name - Python Help - Discussions on Python.org
August 30, 2022 - Suppose i have to set q_variable = 0 a_variable = 0 def stats(choice): choice + '_variable' += 1 stats('q') Here after adding the choice + โ€˜_variableโ€™ will become string. If i want to change the q_variable or tโ€ฆ
Discussions

python - Automatic variable name generation - Stack Overflow
I am not very experienced in python and I need some help. I would like to generate names for some variables automatically, but i don't know how to. Let's say, i have a dict with 20 values and i wan... More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 13, 2019
Automatic creating variables. Proper way with Python
What you want to do is just store those objects into a list. [sympy.Symbol('F{}'.format(x)) for x in range(9)] would do the trick. You'll have to access it like any other list. The next step would be to make it a dict with a dict comprehension: {'F{}'.format(x):sympy.Symbol('F{}'.format(x)) for x in range(9)} so you could just do symbols['F3'] or whatever you name it. More on reddit.com
๐ŸŒ r/learnpython
12
3
February 3, 2016
Go to variable names?
i for integer index iteration. Actual descriptive names for other variables, even for scratch code. Too often have I seen scratch code passed to others, committed, or (worst of all) pushed into production. So proper variable names are a habit I try to enforce unless its literally a file I guarantee will be deleted before any other human sets eyes on it. Even then, if I'm summing something up, I'll always called it summed instead of just s. More on reddit.com
๐ŸŒ r/Python
132
29
January 21, 2024
creating different list names automatically
I am not sure I fully understand what you try to do. But why don't you just create the dict directly? Assuming you have some sort of collection where your actual data comes from. So something like below maybe? data = [ [1, 2, 3], [10, 11, 12], ] result = {} for idx, entry in enumerate(data, start=1): result[f"entry_{idx}"] = entry print(result) Would result in a dict like {'entry_1': [1, 2, 3], 'entry_2': [10, 11, 12]} More on reddit.com
๐ŸŒ r/learnpython
6
1
September 29, 2023
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_variables_names.asp
Python - Variable Names
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python dynamic variable name
Python Dynamic Variable Name | Delft Stack
December 14, 2023 - The combination of the locals() function and the update() method offers another dynamic approach to creating variable names within the local scope. This method is particularly useful when you need to create variables dynamically within a specific ...
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-21151.html
dynamically create variables' names in python
May 14, 2021 - Hi guys, i want to create variables in the following way: assign a name (e.g. var1), then add the name to the prefix of the variable: name = 'var_1' this_is_+name = pd.DataFrame()the outcome i would l
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 3015787 โ€บ how-to-create-a-dynamic-variable-in-python
How to create a dynamic variable in python | Sololearn: Learn to code for FREE!
Lothar Fixed. x = 1 while x < 4: exec("name{}={}" .format(x, x)) x += 1 If You want to test the New variables, write this before the addition: exec("print(name{})" .format(x))
๐ŸŒ
Real Python
realpython.com โ€บ python-variables
Variables in Python: Usage and Best Practices โ€“ Real Python
January 12, 2025 - In this tutorial, you'll learn how to use symbolic names called variables to refer to Python objects, and gain an understanding of how to effectively use these fundamental building blocks in your code to store, manipulate, and retrieve data.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-to-create-dynamically-named-variables-from-user-input
Python program to create dynamically named variables from user input - GeeksforGeeks
July 23, 2025 - Here we are using the exec() method for creating dynamically named variable and later assigning it some value, then finally printing its value.
๐ŸŒ
Quora
quora.com โ€บ How-can-I-dynamically-create-variables-in-Python
How to dynamically create variables in Python - Quora
Answer (1 of 5): x = 0 For i in range(10): String = โ€œvar%d = %dโ€%(x, x) exec(String) x+=1 Now you have 11 variables
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-variables
Python Variables - GeeksforGeeks
Unlike Java and many other languages, Python variables do not require explicit declaration of type. The type of the variable is inferred based on the value assigned. ... Variable names can only contain letters, digits and underscores (_).
Published ย  1 week ago
๐ŸŒ
Readthedocs
natron.readthedocs.io โ€บ en โ€บ v2.3.15 โ€บ devel โ€บ scriptvslabel.html
Python Auto-declared variables โ€” Natron 2.3.15 documentation
For instance, the following variable would not work in Python: >>> my variable = 2 File "<stdin>", line 1 my variable = 2 ^ SyntaxError: invalid syntax ... 1. A script-name: The name that will be used to auto-declare the variable to Python. This name cannot be changed and is set once by Natron ...
๐ŸŒ
Rosetta Code
rosettacode.org โ€บ wiki โ€บ Dynamic_variable_names
Dynamic variable names - Rosetta Code
5 days ago - Enter your variable name: foo Enter your variable value: 42 You have created a variable 'foo' with a value of 42: foo = 42 ... (setq var-name (read)) ; reads a name into var-name (set var-name 1) ; assigns the value 1 to a variable named as entered by the user
๐ŸŒ
DEV Community
dev.to โ€บ chintanonweb โ€บ beyond-static-embracing-dynamic-variable-creation-in-python-57ol
Beyond Static: Embracing Dynamic Variable Creation in Python - DEV Community
April 8, 2024 - One way to dynamically create variables in Python is by utilizing the globals() function. This function returns a dictionary representing the current global symbol table, which contains all the variables defined in the global scope.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ automatic creating variables. proper way with python
r/learnpython on Reddit: Automatic creating variables. Proper way with Python
February 3, 2016 -

Hi,

I have no formal education in programming and I struggle with the idea of "automatic creation of variables". What I understand by that is: a method of putting a name and value in to a variable, I could use later, other than plain assignment with =.

I have come to believe that whenever I feel need to do something like that it is poor design. And I shouldnโ€™t.

But what can I do when, for example, I want to create 9 symbolic variables to play with SymPy. A symbolic variable is done by:

F1 = sympy.Symbol('F1')

Ok, but i want 9 of them and I don't want to write 9 similar lines of code. I figured I can make list of names:

list_of_variables = ['F{}'.format(i) for i in range(9)]

Than something Iโ€™m guessing is wrong (like bad behaviour)

for name in list_of_variables:
    exec(name+' = sm.Symbol('+'name'+')')

It gets me what I want but is it right way to do so ?

๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_variables.asp
Python Variables
Python Variables Variable Names Assign Multiple Values Output Variables Global Variables Variable Exercises Code Challenge Python Data Types
๐ŸŒ
CodeSpeedy
codespeedy.com โ€บ home โ€บ how to create dynamic variable name in python
How to create dynamic variable name in Python - CodeSpeedy
September 18, 2021 - In Python, the for loop and the globals() function are used to construct a dynamic variable name.
๐ŸŒ
Blogger
stupidpythonideas.blogspot.com โ€บ 2013 โ€บ 05 โ€บ why-you-dont-want-to-dynamically-create.html
Stupid Python Ideas: Why you don't want to dynamically create variables
May 22, 2013 - (Someone pointed out to me that Ned Batchelder has a similar post called Keep data out of your variable names . As usual for him, he's got ...