SoFunction
Updated on 2025-03-02

Detailed explanation of examples of converting strings to variable names in Python

introduction

In some cases, you may want to convert a string dynamically to a variable name. In this article, we will explore how to convert strings to variable names in Python with four simple examples.

While Python does not allow direct conversion of strings to variable names, these examples demonstrate various ways to implement similar functions using dictionaries, functions, exec(), or custom classes.

Example 1: Using a dictionary

In this example, we use a dictionary (variable_dict) to associate the string name with the value. We dynamically create a variable name (variable_name) in the form of a string, and then use a dictionary to store and retrieve its value. This method allows us to simulate variable names with string keys.

# Creating a dictionary to store values
variable_dict = {}

# Converting a string into a variable name and assigning a value
variable_name = "my_variable"
variable_value = 42
variable_dict[variable_name] = variable_value

# Accessing the value using the converted string
retrieved_value = variable_dict[variable_name]
print(f"{variable_name}: {retrieved_value}")

Output

my_variable: 42

Example 2: Use globals() and locals()

Here we use the globals() function to create a global variable whose name is defined by the string variable_name. This variable can be accessed throughout the program using the same string as its name.

# Using globals() to create a global variable
variable_name = "my_global_variable"
variable_value = 55
globals()[variable_name] = variable_value

# Accessing the global variable
retrieved_value = globals()[variable_name]
print(f"{variable_name}: {retrieved_value}")

Output

my_global_variable: 55

Example 3: Use exec()

In this example, we use the exec() function to execute a dynamically generated Python code. We build a string containing the name and value of the variable and then execute it. The result is a dynamically created variable that can be accessed by the variable name.

# Converting a string into a variable name using exec()
variable_name = "my_dynamic_variable"
variable_value = 111

# Create the variable dynamically using exec()
exec(f"{variable_name} = {variable_value}")

# Accessing the dynamically created variable
retrieved_value = my_dynamic_variable
print(f"{variable_name}: {retrieved_value}")

Output

my_dynamic_variable: 111

Example 4: Using a custom class

In this example, we create a class called VariableContainer to encapsulate variables. This class provides ways to add and retrieve variables using variable names. By instantiating this class, you can dynamically add and access variables as needed.

# Creating a class with dynamic attributes
class VariableContainer:
	def __init__(self):
		 = {}

	def add_variable(self, name, value):
		[name] = value

	def get_variable(self, name):
		return (name)

# Create an instance of the class
container = VariableContainer()

# Adding variables dynamically
variable_name = "my_dynamic_var"
variable_value = "Hello, World!"
container.add_variable(variable_name, variable_value)

# Accessing the variable
retrieved_value = container.get_variable(variable_name)
print(f"{variable_name}: {retrieved_value}")

Output

my_dynamic_var: Hello, World!

This is the end of this article about the detailed explanation of the example of converting strings into variable names in Python. For more relevant content on converting Python strings into variable names, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!