{"id":26720,"date":"2022-04-01T00:20:53","date_gmt":"2022-03-31T18:50:53","guid":{"rendered":"https:\/\/python-programs.com\/?p=26720"},"modified":"2022-04-01T00:23:47","modified_gmt":"2022-03-31T18:53:47","slug":"how-to-create-dynamic-variable-name-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-create-dynamic-variable-name-in-python\/","title":{"rendered":"How to Create Dynamic Variable Name in Python?"},"content":{"rendered":"
A dynamic variable name, often known as a variable, is a variable whose name is an estimation of another variable.<\/p>\n
It is nothing but a user-given name.
\nDespite the fact that Python is a highly dynamic language in which almost everything is an object, dynamic variables can be created in Python.<\/p>\n
We use the below methods to create a dynamic variable name:<\/p>\n
globals() function in python:<\/strong><\/p>\n The globals() function in Python returns a dictionary that contains the current global symbol table.<\/p>\n The globals() method retrieves the current global symbol table’s dictionary.<\/p>\n A symbol table is a data structure maintained by a compiler that contains all relevant program information.<\/p>\n Variable names, methods, classes, and so forth are examples of this.<\/p>\n There are two types of symbol tables:<\/p>\n locals() function in python:<\/strong><\/p>\n The local symbol table maintains all information relating to the program’s local scope and is accessed in Python via the locals() function.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" 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 …<\/p>\n\n
Creating Dynamic Variable Name in Python<\/h2>\n
\n
Method #1: Using globals() function<\/h3>\n
\n
# Give some random dynamic variable name as static input and store it in a variable.\r\ndynamicvariable= \"hello\"\r\n\r\n# Assign the above dynamic variable name with some other random name using the \r\n# globals() function \r\nglobals()[dynamicvariable] = \"Python-programs\"\r\n\r\n# Print the value of the dynamicvariable which is initialized at the first.\r\nprint(hello)<\/pre>\n
Python-programs<\/pre>\n
Method #2: Using locals() function<\/h3>\n
\n
# Give some random dynamic variable name as static input and store it in a variable.\r\ndynamicvariable= \"hello\"\r\n\r\n# Assign the above dynamic variable name with some other random name using the \r\n# locals() function \r\nlocals()[dynamicvariable] = \"Python-programs\"\r\n\r\n# Print the value of the dynamicvariable which is initialized at the first.\r\nprint(hello)<\/pre>\n
Python-programs<\/pre>\n
Method #3: Using exec() function<\/h3>\n
\n
# Give some random dynamic variable name as static input and store it in a variable.\r\ndynamicvariable= \"hello\"\r\n\r\n# Assign the above dynamic variable name with some other random name using the \r\n# exec() function \r\nexec(\"%s = %d\" % (dynamicvariable, 2011))\r\n\r\n# Print the value of the dynamicvariable which is initialized at the first.\r\nprint(hello)<\/pre>\n
2011<\/pre>\n
Method #4: Using vars() function<\/h3>\n
\n
# Give some random dynamic variable name as static input and store it in a variable.\r\ndynamicvariable= \"welcome\"\r\n\r\n# Assign the above dynamic variable name with some other random name using the \r\n# vars() function \r\nvars()[dynamicvariable] = \"Python-programs\"\r\n\r\n# Print the value of the dynamicvariable which is initialized at the first.\r\nprint(welcome)<\/pre>\n
Python-programs<\/pre>\n