Convert String into Variable name in Python

Ever wondered about converting a user input string into a variable name in Python. In this tutorial, we are going to learn how to Convert User Input String into Variable name using Python.
In other words, we are creating dynamic variable names and assigning a value to it.

Here, the task is to-
Create a variable with a user-defined name.
The variable name should not be written in the program text but should be taken from the user dynamically.

You might have a question, why do we need to do this? Why can’t we just declare them normally? You will get the answer to those questions at the end of the tutorial. Now let’s look at the dynamic declaration of the variable name.



Declaring Variable Name Dynamically

To convert the user input string into variable name in Python, initially, we have to take input from the user and store it in a variable named user_input-
In Python 2.x:

user_input = raw_input("Enter a variable name: ")

Output:

Enter a variable name: number

In Python 3.x:

user_input = input("Enter a variable name: ")

Output:

Enter a variable name: number

Now, by using globals() or locals() function, we can change the string into a variable name and assign a value to it.
In Python, globals() / locals() function returns a dictionary containing the variables defined in the global / local namespace.
One of the features of globals() is that we can modify the variables in the global dictionary. Using this feature we are going to assign a value to the variable(which is created dynamically by the user). Since it’s a dictionary, we can get the value of a specific key too. locals() values can also be altered since it’s a dictionary, but it’s not recommended.

globals()[user_input]=25 #variable is dynamically created and is assigned to 25
print(number)          #dynamically created variable's value is printed
type(number)           #To know the type of the variable user created

Output:

25
int

Let’s take another example:

user_input = input("Enter a variable name: ")

Output:

Enter a variable name: CodeSpeedy
globals()[user_input] = "It's a good website to learn about technology" #variable is dynamically created and is assigned to 25
print(CodeSpeedy)          #dynamically created variable's value is printed
type(CodeSpeedy)           #To know the type of the variable user created

Output:

It's a good website to learn about technology
str

Limitation: The above-specified technique cannot be used inside a method.

Pros and Cons of Dynamically Creating Variables in Python

Pros:

  • Dynamic creation of variables adds another level of indirection. Thus making the environment more dynamic.
  • It allows avoiding more code duplication.

Cons:

  • Creating dynamic variables is not applicable for functions.
  • It is hard to keep track of ‘lexical references’:
    i. if created variable names are arbitrary, conflicts can occur.
    ii. it’s difficult to predict the behavior and find inputs and outputs in the code.

Also, read:

 

2 responses to “Convert String into Variable name in Python”

  1. Purnendu says:

    globals()[user_input] = “It’s a good website to learn about technology” #variable is dynamically created and is assigned to 25

    it is a wrong comment please check.
    it will be:
    #variable is dynamically created and is assigned to “It’s a good website to learn about technology”

  2. Purnendu says:

    This technique is quite good. The alternative way to use a python dictionary. And the dynamic variable name will be a key and value can be assigned externally.

    code:

    temp_store = {}
    var_name = input(“Type a variable name: “)
    temp_store[var_name] = “Hello World !”
    print(temp_store[‘new_var’])

    Input/Output:

    Type a variable name: new_var
    Hello World !

Leave a Reply

Your email address will not be published. Required fields are marked *