How to Create Dynamic Variable Name in Python?

A dynamic variable name, often known as a variable, is a variable whose name is an estimation of another variable.

It is nothing but a user-given name.
Despite the fact that Python is a highly dynamic language in which almost everything is an object, dynamic variables can be created in Python.

We use the below methods to create a dynamic variable name:

  • Using globals() function
  • Using locals() function
  • Using exec() function
  • Using vars() function

globals() function in python:

The globals() function in Python returns a dictionary that contains the current global symbol table.

The globals() method retrieves the current global symbol table’s dictionary.

A symbol table is a data structure maintained by a compiler that contains all relevant program information.

Variable names, methods, classes, and so forth are examples of this.

There are two types of symbol tables:

  • Local symbol table
  • Global symbol table

locals() function in python:

The local symbol table maintains all information relating to the program’s local scope and is accessed in Python via the locals() function.

Creating Dynamic Variable Name in Python

Method #1: Using globals() function

Approach:

  • Give some random dynamic variable name as static input and store it in a variable.
  • Assign the above dynamic variable name with some other random name using the globals() function
  • Print the value of the dynamic variable which is initialized at the first.
  • The Exit of the Program.

Below is the implementation:

# Give some random dynamic variable name as static input and store it in a variable.
dynamicvariable= "hello"

# Assign the above dynamic variable name with some other random name using the 
# globals() function 
globals()[dynamicvariable] = "Python-programs"

# Print the value of the dynamicvariable which is initialized at the first.
print(hello)

Output:

Python-programs

Method #2: Using locals() function

Approach:

  • Give some random dynamic variable name as static input and store it in a variable.
  • Assign the above dynamic variable name with some other random name using the locals() function
  • Print the value of the dynamic variable which is initialized at the first.
  • The Exit of the Program.

Below is the implementation:

# Give some random dynamic variable name as static input and store it in a variable.
dynamicvariable= "hello"

# Assign the above dynamic variable name with some other random name using the 
# locals() function 
locals()[dynamicvariable] = "Python-programs"

# Print the value of the dynamicvariable which is initialized at the first.
print(hello)

Output:

Python-programs

Method #3: Using exec() function

Approach:

  • Give some random dynamic variable name as static input and store it in a variable.
  • Assign the above dynamic variable name with some other random name(here we used number) using the exec() function
  • Print the value of the dynamic variable which is initialized at the first.
  • The Exit of the Program.

Below is the implementation:

# Give some random dynamic variable name as static input and store it in a variable.
dynamicvariable= "hello"

# Assign the above dynamic variable name with some other random name using the 
# exec() function 
exec("%s = %d" % (dynamicvariable, 2011))

# Print the value of the dynamicvariable which is initialized at the first.
print(hello)

Output:

2011

Method #4: Using vars() function

Approach:

  • Give some random dynamic variable name as static input and store it in a variable.
  • Assign the above dynamic variable name with some other random name using the vars() function
  • Print the value of the dynamic variable which is initialized at the first.
  • The Exit of the Program.

Below is the implementation:

# Give some random dynamic variable name as static input and store it in a variable.
dynamicvariable= "welcome"

# Assign the above dynamic variable name with some other random name using the 
# vars() function 
vars()[dynamicvariable] = "Python-programs"

# Print the value of the dynamicvariable which is initialized at the first.
print(welcome)

Output:

Python-programs