Sure you can; it's called a dictionary:

d = {}
for x in range(1, 10):
    d["string{0}".format(x)] = "Hello"
>>> d["string5"]
'Hello'
>>> d
{'string1': 'Hello',
 'string2': 'Hello',
 'string3': 'Hello',
 'string4': 'Hello',
 'string5': 'Hello',
 'string6': 'Hello',
 'string7': 'Hello',
 'string8': 'Hello',
 'string9': 'Hello'}

I said this somewhat tongue in check, but really the best way to associate one value with another value is a dictionary. That is what it was designed for!

Answer from the wolf on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › trying to create a loop to create dynamic variables
r/learnpython on Reddit: Trying to create a loop to create dynamic variables
January 23, 2022 -

Hi, I think I understand how to do this high level, but was hoping for help that was a little more specific.

From my understanding, creating dynamic variables goes like this:

for i in range(0, 9):

globals()[f"my_variable{i}"] = f"Hello from variable number {i}!

However, what if I want to make a variable that says server1, server2...server9- how do i attach the word to the beginning?

Discussions

Dynamic variable name
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 the a_variable depending on the choice we provide to the function. How do we do that? More on discuss.python.org
🌐 discuss.python.org
10
1
August 30, 2022
python - How can you dynamically create variables? - Stack Overflow
I want to create variables dynamically in Python. Does anyone have any creative means of doing this? More on stackoverflow.com
🌐 stackoverflow.com
Is it possible to create variables in a loop?
You can create a dictionary or a list to save and easily access all these: # dictionary d = {} for i in range(1, 1000): d["var{0}".format(i)] = 0 # list var = [] for i in range(1, 1000): var.append(0) More on reddit.com
🌐 r/learnpython
5
2
June 10, 2022
How to creating different variable name and through for loop?
You don't. There is no need to do this. Use a list. More on reddit.com
🌐 r/learnpython
10
2
March 27, 2024
🌐
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!
Edward Marais , to do this task we can generate the variable names like *var* + a number inside a loop. then we have to add this name together with the value we like to assign to the locals() dict.
🌐
GeeksforGeeks
geeksforgeeks.org › python › changing-variable-names-with-python-for-loops
Changing Variable Names With Python For Loops - GeeksforGeeks
July 23, 2025 - In this example, below Python code below adds the prefix "var_" to each variable name in the list original_names. It uses a for loop to iterate through the original names, creates new names with the prefix, and appends them to the list prefixed_names.
🌐
Plain English
plainenglish.io › home › blog › python › how to dynamically declare variables inside a loop in python
How to Dynamically Declare Variables Inside a Loop in Python
July 18, 2021 - But as time goes on, our code will ... a suitable variable name. Then we name them x, y, and so on. The problem becomes more complicated when you need to declare variables dynamically in a loop. I came up with three ways to do this in Python....
🌐
cyberangles
cyberangles.org › blog › using-a-loop-in-python-to-name-variables
Python Loop for Dynamic Variable Names: How to Create double_1, double_2...double_12 — CyberAngles.org
You can modify these dictionaries to create dynamic variables in the global or local scope. Warning: This method is not recommended because it pollutes the namespace and can accidentally overwrite existing variables. globals() returns a dictionary of the current global namespace. By adding a key-value pair to this dictionary, you create a global variable. # Loop from 1 to 12 to create global variables for i in range(1, 13): # Generate the variable name (e.g., "double_3") var_name = f"double_{i}" # Assign the value (2 * i) to the global variable globals()[var_name] = 2 * i # Access the variables like regular global variables print(double_7) # Output: 14 print(double_12) # Output: 24
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › python › python dynamic variable name
Python Dynamic Variable Name | Delft Stack
December 14, 2023 - In the first loop, for n in range(0, 7), we iterate over the numbers from 0 to 6. Inside the loop, globals()["dynamic_var%s" % n] = "Hello" creates dynamic variables named dynamic_var0 through dynamic_var6, each initialized with the string Hello.
🌐
Quora
quora.com › How-do-I-create-a-loop-that-creates-variables-in-Python
How to create a loop that creates variables in Python - Quora
Answer (1 of 6): You don’t. Now I know someone will come along with a solution involving locals(), but then to use those ‘variables’ in your code you would need use ‘locals()’. Misusing locals() especially writing to it is a sign of a bad design. It is far simpler just to add items to a diction...
🌐
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 locals() method for creating dynamically named variable and later assigning it some value, then finally printing its value. ... # Dynamic_Variable_Name can be # anything the user wants.
🌐
sqlpey
sqlpey.com › python › top-8-methods-to-dynamically-create-variable-names-in-python-loops
Top 8 Methods to Dynamically Create Variable Names in Python Loops
November 6, 2024 - While creating variable names dynamically via globals() is feasible, it is considered bad practice. This approach involves modifying the global namespace, which can lead to unintended issues. for x in range(0, 9): globals()['string%s' % x] = 'Hello' print(string3) # Output: Hello · The exec() function allows you to execute dynamically created Python statements, but like globals(), it carries risks and makes your code harder to follow.
🌐
CodeSpeedy
codespeedy.com › home › how to create dynamic variable name in python
How to create dynamic variable name in Python - CodeSpeedy
September 18, 2021 - # Use a for loop to create dynamic variable names and assign values for x in range(0, 7): variable_name = f”variable1{x}” variable_value = f”Hello CodeSpeedy Student {x}!!!” dynamic_variables[variable_name] = variable_value
🌐
Esri Community
community.esri.com › t5 › python-questions › create-dynamic-variable-from-returned-value › td-p › 1185872
Solved: Create dynamic variable from returned value / vari... - Esri Community
July 7, 2022 - You can use __getattr__() and __setattr__() built in methods on global/local namespaces to get/set variables by string name on an object or module. See: https://docs.python.org/3.7/library/functions.html#getattr and https://docs.python.org/...
🌐
Medium
medium.com › geekculture › a-cool-way-to-dynamically-create-variables-in-python-7c20c12f4f23
A Cool Way To Dynamically Create Variables In Python | by Liu Zuo Lin | Geek Culture | Medium
April 17, 2023 - A Cool Way To Dynamically Create Variables In Python When we create variables in Python, these variables are actually stored in a dictionary. And we can get this dictionary using globals(). a = 100 b …
🌐
Python Forum
python-forum.io › thread-23500.html
Changing a variable's name on each iteration of a loop
Is it possible in python to change the name of a variable on each iteration of a loop? For example: for i in range(10): variableNameToChange+i='iterationNumber=='+str(i)I know this won't work, and you can't assign to an operator, but how would y...
🌐
MP4Moviez
pakainfo.com › home › python › python dynamic variable name – how to create a dynamic variable name in python?
Python Dynamic Variable Name - How To Create A Dynamic Variable Name In Python? - Pakainfo
for i in range(0, 9): globals()[f"my_variable{i}"] = f"Welcome from variable number {i}!" print(my_variable3) # Welcome from variable number 3! for x in range(0, 9): globals()['string%s' % x] = 'Welcome' # string0 = 'Welcome', string1 = 'Welcome' ... string8 = 'Welcome' name = "a" value = True player_message = {name: value} print(player_message["a"]) Don’t Miss : How To Read And Write Files In Python 3? I hope you get an idea about python dynamic variable name.
🌐
Reddit
reddit.com › r/learnpython › is it possible to create variables in a loop?
r/learnpython on Reddit: Is it possible to create variables in a loop?
June 10, 2022 -

I need to create a large number of variable. Currently what I have is:

var1 = 0
var2 = 0
...
var1000 = 0

Is it possible to create these variables in some sort of loop instead of writing each one?

#Pseudocode

for i in range(1000):
    var_i = 0

Update:

Thanks a lot for all the comments! The list idea worked!

🌐
Quora
quora.com › How-do-you-create-different-variable-names-while-in-a-loop-in-Python
How to create different variable names while in a loop in Python - Quora
Answer (1 of 5): There is a way: [code]>>> exec("A = 3") >>> A 3 [/code]… meaning in a for loop you could synthesize some varnames and make them globals. However said practice is heavily frowned upon. Best practice is to create a dict and instead of a plethora of names polluting your namespace,...