Skip to content
Home » Convert a String to Variable Name in Python

Convert a String to Variable Name in Python

In this tutorial let’s learn about How to Convert a String to Variable Name in Python. In Python, there are several methods for converting a string value to a variable name. This lesson will solely cover the topic of converting a Python string to a variable name. In other words, we’ll be generating dynamic variable names and assigning them values. There are several methods for converting a string to a variable name. This post will go over all of them.

We will use the exec() function, the locals() function, and the globals() function to convert a string to a variable name.

String to Variable Name Using exec() Method in Python

The exec() method allows us to dynamically execute a Python program.

In this example, we’ll have a variable that we’ve named and an input string. Then we’ll use the exec() method with some modifications to try to transform a string to a variable name. Let’s take a closer look with the help of the following example:

x = "temp"
 
# exec() method
exec("%s = %d" % (x,10))

print("The temp value : ",temp) 

Output:

The temp value : 10

As seen in the above code snippet, we have % s and % d inside the exec() function, which are placeholders for string and decimal values, respectively. It indicates that we use the assignment operator = to assign an integer value to a string. Both % s and % d are wrapped in quotation marks “”. Then we have a parentheses with two items inside: the first is the variable we created, x, which has the value temp, and the second is an integer 10.

So the value 10 is assigned to the value of the variable x i.e., 10 is assigned to temp.

Convert String to Variable Name Using locals() in Python

Python’s locals() method returns the dictionary of the current local symbol table. The locals() function is used to access the local symbol table. This function functions in the same way as the globals() function. The only difference is that locals() accesses the local symbol database whereas globals() accesses the global symbol table, and both return the dictionary.

Code Example: In this example, we’ll use a string as the input. Then, by assigning a value to the locals, we will use them as a dictionary.

#input string
x = "temp"
 
locals()[x] = 10
print('The temp value :',temp)

Output:

The temp value : 10

Convert String to Variable Name Using globals() in Python

The globals() function is used to return the current symbol table’s dictionary. A global symbol table, accessed via the globals() function, is used to hold all information relating to the program’s global scope.

Code Example:

#input string
x = "temp"
 
globals()[x] = 10
print('The temp value :',temp)

Output:

The temp value : 10

Conclusion

In this tutorial we learnt about converting a string into variable name using python. We learnt about different methods like exec() , locals() and globals() for converting the string into variable name.

Similar Posts:

Remove Commas From String in Python
Extract Text From Image Using pytesseract in Python