. Advertisement .
..3..
. Advertisement .
..4..
Python is a multi-object language that implements the level data model form of the great academic libraries system. How to convert a string to variable name in python? It’s one of the most common programming questions. We’ll collaborate to find the best solutions.
How to convert a string to variable name in python
There are several Python methods for converting a string value to a variable name. This post would go over different methods for accomplishing this. Some people may want to do this to define a variable name dynamically while the Python program is running.
In addition, converting a string to a variable name using the methods shown below is not recommended when developing production-ready software because it can lead to problems. Yes, it is possible to do that, but please proceed with caution.
Method 1: Using the locals()
The simplest way to convert a string to variable name in Python is using locals(). Python’s locals() method provides the dictionary for the active local symbol table. The locals() function can be used to access a local symbol table.
Let’s learn more about it through this instance:
str = "rolax"
locals()[str] = 8000
print(rolax)
Output:
8000
Method 2: Using the exec()
Another way to convert a string to variable name in python is using exec(). The Python program is dynamically executed using the exec() function.
str = "rolax"
exec("%s = %d" % (str,8000))
print("output : ",rolax)
Output:
8000
Method 3: Using the globals()
You could really easily switch by using globals(). The global symbol table’s dictionary is returned by Python’s globals() function. The globals() function provides access to the global symbol table, which contains all the data pertaining to the program’s global scope. For example:
str = "rolax"
globals()[str] = 8000
print(rolax)
Output:
8000
Method 4: Using the vars()
In Python, you have the option of utilizing the vars() function in place of the locals() and globals() functions to change a string to a variable name. When used in the global scope, the vars() function works exactly like the globals() function. The vars() function acts similarly to the locals() function when called within a function or an inner scope. Let’s look at the following example:
myStr = "domain"
print("The string is:",myStr)
myVars = vars()
myVars[myStr] = "pythonforbeginners.com"
print("The variables are:")
print(myVars)
print("{} is {}".format(myStr, domain))
Output:
The string is: domain
The variables are:
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__':
<_frozen_importlib_external.SourceFileLoader object at 0x7fb9c6d614c0>,
'__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins'
(built-in)>, '__file__': '/home/aditya1117/PycharmProjects/pythonProject/string12.py',
'__cached__': None, 'myStr': 'domain', 'myVars': {...}, 'domain':
'pythonforbeginners.com'}
domain is pythonforbeginners.com
Conclusion
In general, Python is a high-level programming language that is object-oriented and provides automatic memory resources. It also has many beginner-friendly features. You can always read this post again when you’re still stuck on “How to convert a string to variable name in Python“. The options mentioned above are the quickest. In addition, if you still need help or have problems, we have a large population where everyone is usually eager to help. Finally, we wish all of you a wonderful day full of new code alternatives and appreciate your reading time.
Read more
Let’s read this useful article to open your knowledge about Python:
Leave a comment