3 Ways to Convert String to Variable Name in Python

Introduction

We got to know about many topics in python. But, have you ever use the input string as the variable name in your program. In this tutorial, we will be focusing on this topic only to convert a Python string to a variable name. In other words, we will be creating dynamic variable names and assigning a value to them. There are multiple ways to convert a string to a variable name. We will discuss all of them in this article.

We will convert a string into a variable name with multiple ways like exec() function, locals() function and globals() function.

Multiple Ways With Examples to Convert a Python string to a Variable Name

We will then discuss how we can convert a string to a variable name with examples explained in detail.

1. Using exec() Method in Python to Convert a Python string to a Variable Name

The Exec() methods helps us to execute the python program dynamically.

In this example, we will have a variable named by us and an input string. We will then be applying the exec() method with some modifiers and trying to convert a string into a variable name. Let us look in detail with the help of the example explained below:

#input String
str = "Pythonpool"

#apply exec() method
exec("%s = %d" % (str,5000))

#print string
print("output : ",Pythonpool) 

Output:

output :  5000

Explanation:

  • Firstly, we have taken an input string in str as Pythonpool.
  • Then, we have applied the exec() function.
  • Inside the exec() function, we have taken %s and %d, which are used as a placeholder for string value and decimal value, respectively. It means that we have assigned 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 parenthesis inside which we have passed 2 things, i.e., string and integer.
  • At last, we have printed the input string and see if the string contains a variable name or not.
  • If the program runs successfully and prints the output, we have converted a string into a variable name.
  • Hence, you can see the output.

2. Using locals() function to Convert a Python string to a Variable Name

The locals() function in python is used to return the dictionary of the current local symbol table. The local symbol table is accessed with the help of the locals() function. This function works the same as the globals() function. The only difference between them is the locals() function access local symbol table, and globals() access the global symbol table, and both return the dictionary.

In this example, we will be taking the input as a string. Then, we will apply the locals as a dictionary by putting some value to it. At last, we will print the input string and convert a string into a variable name.

#taking input as a string
str = "pythonpool"

locals()[str] = 5000
print(pythonpool)

Output:

5000

Explanation:

  • Firstly, we have taken an input in str as pythonpool.
  • Then, we have modified the value of the given string through the local’s dictionary through the locals() function.
  • At last, we have printed the input string and see if the string contains a variable name or not.
  • If the program runs successfully and prints the output, we have converted a string into a variable name.
  • Hence, you can see the output.

3. Using globals() function to Convert a Python string to a Variable Name

The globals() function is used to return the dictionary of the current symbol table. A global symbol table is used to store all the information related to the program’s global scope, accessed using the globals() function.

In this example, we will be taking the input as a string. Then, we will apply the globals as a dictionary by putting some value to it. At last, we will print the input string and convert a string into a variable name.

#taking input as a string
str = "pythonpool"

globals()[str] = 5000
print(pythonpool)

Output:

5000

Explanation:

  • Firstly, we have taken an input in str as pythonpool.
  • Then, we have modified the value of the given string through the globals dictionary through the globals() function.
  • At last, we have printed the input string and see if the string contains a variable name or not.
  • If the program runs successfully and prints the output, we have converted a string into a variable name.
  • Hence, you can see the output.

Pros and cons of creating Global variables in python

Pros

  • When we create dynamic variables, they add another level of indirection.
  • It avoids more code duplication.

Cons

  • We cannot create dynamic variables for functions.
  • It isn’t easy to keep all the tracks of lexical references:
    • if we create arbitrary variable names, conflicts can occur.
    • It is difficult to predict the behavior and find inputs and outputs in the code.

Must Read

Conclusion

In this tutorial, we have learned about the concept of converting a string to a variable name. We have discussed all the ways through which we can convert a string into a variable name. All the ways are explained in detail with the help of examples. You can use any of the methods which you find suitable for you and your program.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments