You can use exec for that:

>>> foo = "bar"
>>> exec(foo + " = 'something else'")
>>> print bar
something else
>>> 
Answer from Jack Leow on Stack Overflow
๐ŸŒ
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).
๐ŸŒ
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: ... ... Edward Marais , to do this task we can generate the variable names like *var* + a number inside a loop....
๐ŸŒ
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.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python dynamic variable name
Python Dynamic Variable Name | Delft Stack
December 14, 2023 - In this output, the dynamically created variable variable5 is printed, demonstrating how the for loop and globals() function can be utilized to generate dynamic variable names in Python.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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?
๐ŸŒ
Real Python
realpython.com โ€บ python-variables
Variables in Python: Usage and Best Practices โ€“ Real Python
January 12, 2025 - By associating a variable with a value, you can refer to the value using a descriptive name and reuse it as many times as needed in your code. Variables behave as if they were the value they refer to. To use variables in your code, you first need to learn how to create them, which is pretty straightforward in Python.
๐ŸŒ
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.
๐ŸŒ
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 - I will actually need something like {colour} + '_dom' to be the new variable name as {colour} changes within a loop. ... Solved! Go to Solution. ... That sort of hackery is not very pythonic.
๐ŸŒ
Rosetta Code
rosettacode.org โ€บ wiki โ€บ Dynamic_variable_names
Dynamic variable names - Rosetta Code
1 week 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
๐ŸŒ
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 - You may keep the scope of the variable local by using the locals() function to generate dynamically named variables, which can enhance the programโ€™s efficiency and security. Another benefit is that it could make writing less code to construct a variable easier. The fact that local variables may only be accessible within the current function or block of code, however, limits their use in some circumstances. A string can be executed as Python code using the built-in Python function exec().
๐ŸŒ
sqlpey
sqlpey.com โ€บ python โ€บ top-4-techniques-to-dynamically-generate-variable-names-in-python
Top 4 Techniques to Dynamically Generate Variable Names in Python
November 23, 2024 - While this method effectively creates global variables, itโ€™s often frowned upon in Python programming due to the potential for namespace pollution and decreased code clarity. Another option is to use locals(). However, this method is less recommended for generating dynamic variables as it tends to obscure the codeโ€™s intent.
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ dynamic-ways-creating-variables-python-akhil-pathirippilly-mana
Dynamic ways of creating variables in python
February 11, 2022 - ) 3. Using setattr built-in method: setattr(obj, name, value) will call self.__setattr__ dunder method and will set value assigned to "name" argument as attribute name and value assigned to "value" argument as its original value.
๐ŸŒ
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
๐ŸŒ
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
๐ŸŒ
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