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
🌐
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....
Discussions

Trying to create a loop to create dynamic variables
globals()[f"server{i}"] Don't do that though. Use a dictionary, or a list. More on reddit.com
🌐 r/learnpython
7
1
January 23, 2022
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
python - Dynamically create variables in for loop - Stack Overflow
I am trying to dynamically create variables in a for loop based on a count. I'm using the openpyxl module to iterate over a worksheet. value = ~sheet name after using a loop to iterate over sheet More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 ... 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....
🌐
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?

🌐
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...
🌐
Delft Stack
delftstack.com › home › howto › python › python dynamic variable name
Python Dynamic Variable Name | Delft Stack
December 14, 2023 - When combined with the globals() function, which provides access to the global symbol table as a dictionary, you can dynamically create variables. In the following example, we will use the for loop to iterate over a range of numbers.
🌐
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
This blog will guide you through **how to create dynamic variable names like `double_1` to `double_12` using Python loops**, explore different methods (including the recommended approach), and explain best practices to avoid common pitfalls. By the end, you’ll understand when to use dynamic ...
Find elsewhere
🌐
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...
🌐
Plain English
python.plainenglish.io › how-to-dynamically-declare-variables-inside-a-loop-in-python-21e6880aaf8a
How to Dynamically Declare Variables Inside a Loop in Python | by Naveenkumar M | Python in Plain English
August 9, 2024 - . Then I assumed the values from the for loop to the dynamic variables. The dictionary will look like · {‘x0’: 0, ‘x1’: 1, ‘x2’: 2, ‘x3’: 3, ‘x4’: 4, ‘x5’: 5, ‘x6’: 6, ‘x7’: 7, ‘x8’: 8, ‘x9’: 9} You can learn about dictionaries here. ... Finally, I iterated over the dictionary and separated the keys into a standalone variable using the exec command. The exec command executes the Python code inside it.
🌐
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
🌐
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!

🌐
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.
🌐
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' ...
🌐
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 - if sorted_output[0][1] == '0': (captures null/zero tuples) dom = '0' else: most_significant_output = sorted_output[0] (captures ranked dominant tuple) dom = most_significant_output[0] (section where I need a variable in the loop to be named sedan_dom, wagon_dom, convertible_dom)
🌐
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 - def switch_name(branchNum): switches = [] for x in range(1, 8): switches.append("BR" + str(branchNum) + "SW0" + str(x)) for switch in switches: print(switch) # Output: BR123SW01, BR123SW02, ... def main(): branchNum = "123" switch_name(branchNum) if __name__ == '__main__': main() We’d love to hear your thoughts! Did you find these methods useful? Are there other techniques you have used? Please share your feedback or any questions below. A: Dynamic variable names refer to the ability to create variable names programmatically, often within loops or functions, rather than defining them statically in code.
🌐
Codingdeeply
codingdeeply.com › home › python: 5 best techniques to generate dynamic variable names
Python: 5 Best Techniques to Generate Dynamic Variable Names - Codingdeeply
February 23, 2024 - Finally, dynamically named variables may be useful in Python programming since they allow developers to define variables with arbitrary names during runtime. Developers can construct dynamic variable names that improve code readability and maintainability by utilizing techniques like globals(), locals(), exec(), for loops, or dictionaries.
🌐
Reddit
reddit.com › r/learnpython › how to use a dynamic variable in python loop?
r/learnpython on Reddit: how to use a dynamic variable in python loop?
March 18, 2021 -

Hello

I'm new to python. I'm trying to do the following thing

var2 = re.findall(r'fixedtext1.*?'+var1+'.*?fixedtext2(.*?)fixedtext3.*?row:2', data)[0]
var3 = re.findall(r'fixedtext1.*?'+var2+'.*?fixedtext2(.*?)fixedtext3.*?row:3', data)[0]
var4 = re.findall(r'fixedtext1.*?'+var3+'.*?fixedtext2(.*?)fixedtext3.*?row:4', data)[0]
var5 = re.findall(r'fixedtext1.*?'+var4+'.*?fixedtext2(.*?)fixedtext3.*?row:5', data)[0]
var6 = re.findall(r'fixedtext1.*?'+var5+'.*?fixedtext2(.*?)fixedtext3.*?row:6', data)[0]

In the above, if there is row:2 (in the end) there will be var1 (output of var1) in the middle and for row:3 there will be var2 (output of var) in middle and so on. var1 is independent and from var2 the fixed part will start using var1. I need to do like this for 30 items (var30). I could loop the end part (row:) with loop but I don't know how to loop variable in the middle while it is changing for every line but in fixed behavior. Is there anyway to simplify this with looping? please help