Convert a String to Variable Name in Python
-
Convert String to Variable Name Using
globals()
andlocals()
in Python -
Convert String to Variable Name Using
exec()
in Python

In Python, there are various ways in which we can convert a string value into a variable name. In this article, we will discuss various ways in which we can achieve this.
Some people might want to do this to define a variable name dynamically while the Python program is under execution. Converting a string to a variable name using the ways shown below is not recommended while developing production-ready software because this can cause some issues. Yes, it is possible to achieve this, but please be cautious while doing this.
Convert String to Variable Name Using globals()
and locals()
in Python
globals()
The globals()
function in Python returns the dictionary of the current global symbol table. A global symbol table stores all the information related to the program’s global scope, which can be accessed using the globals()
function.
user_input = input("Enter string for variable name: \n")
globals()[user_input] = 50
print(apple)
print(type(apple))
Output:
The first thing we are doing here is to take input from the user using the input()
function and passing a string Enter a string for a variable name:
inside it. Then whatever value a user will enter, that value will be stored inside the variable user_input
. Here, we expect that the user will enter apple
as a string value which will then be stored inside user_input
. If the user enters a value other than apple
, the program will through an error as NameError: name 'variable' is not defined
. It is because we are also printing the value of apple
and then checking the type of it later in our code.
If the user has entered the correct value, we can modify the global dictionary using globals()
to modify the global dictionary and assign any value to the variable apple
. Here, we have assigned 50
to the variable apple
.
locals()
The locals()
function in Python returns the dictionary of the current local symbol table. A local symbol table can be accessed with the help of the locals()
function. The locals()
function almost works the same way as the globals()
function. The only difference is that locals()
can access the local symbol table, and globals()
can access the global symbol table and returns a dictionary.
user_input = input("Enter string for variable name: \n")
locals()[user_input] = 50
print(apple)
print(type(apple))
Output:
This code also works the same way as that of globals()
. The only difference is that we have used the function locals()
here.
Convert String to Variable Name Using exec()
in Python
Another way of doing this thing is by using the exec()
function in Python. The exec()
function is used to execute the Python program dynamically. We have a variable name
inside which we are storing a string value Elon
.
name = "Elon"
exec("%s = %d" % (name, 100))
print(Elon)
Output:
100
Inside the exec()
function, we have %s
and %d
, used as a placeholder for string value and decimal value, respectively. It means that we assign an integer value to a string with the help of the assignment operator =
. Both %s
and %d
are enclosed inside the quotations ""
. Then we have a parentheses inside which we have 2 things, the first is the variable we have created i.e name
, which contains the value Elon
, and the second we have an integer 100
.
Here, instead of the name
variable, the value inside that variable will be substituted. The integer 100
will be assigned to Elon
. Elon
becomes a variable name, and if you print it, you will get 100
as the output.
Sahil is a full-stack developer who loves to build software. He likes to share his knowledge by writing technical articles and helping clients by working with them as freelance software engineer and technical writer on Upwork.
LinkedInRelated Article - Python String
- Remove Commas From String in Python
- Check a String Is Empty in a Pythonic Way
- Remove Whitespace From a String in Python
- Extract Numbers From a String in Python
- Convert String to Datetime in Python