3 Ways to Convert String to Variable Name in Python

Introduction

We got to know about many topics in Python. But have you ever used the input string as the variable name in your program? This tutorial will focus on the topic of converting a Python string to a variable name. In other words, we will create dynamic variable names and assign 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 in 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 to convert a string to a variable name, with detailed examples.

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 apply the exec() method with some modifiers and try 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 placeholders for string and decimal values, 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 printed the input string and saw if the string contained a variable name.
  • If the program successfully 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 accesses the local symbol table, and globals() accesses 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 adding 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 modified the value of the given string through the local 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 stores 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 adding some value. 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 modified the value of the given string through the globals dictionary through the globals() function.
  • At last, we printed the input string and saw if the string contained a variable name.
  • If the program successfully 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.
    • Predicting the behavior and finding inputs and outputs in the code is difficult.

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 questions, please let me know in the comment section below. I will try to help you as soon as possible.

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Audrey Vasquez
Audrey Vasquez
1 year ago

Hi!
I appreciate this help. I’m using the globals() option, which is working but it’s still printing out my initial string rather than the new set variable. although when i print out the actual variable (by calling the name of it that the string is) it prints out the value i set it to. so it works, but for purposes of making it more universal i didnt plan on writing out what the string is because i want it to be used for many different types of strings, any ideas? thank you!