Python program to create dynamically named variables from user input Last Updated : 13 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string input, our task is to write a Python program to create a variable from that input (as a variable name) and assign it to some value. Below are the methods to create dynamically named variables from user input. Using globals() method to create dynamically named variables Here we are using the globals() method for creating a dynamically named variable and later assigning it some value, then finally printing its value. Python3 # Dynamic_Variable_Name can be # anything the user wants Dynamic_Variable_Name = "geek" # The value 2020 is assigned # to "geek" variable globals()[Dynamic_Variable_Name] = 2020 # Display variable print(geek) Output: 2020Using locals() method to create dynamically named variables Here we are using the locals() method for creating dynamically named variable and later assigning it some value, then finally printing its value. Python3 # Dynamic_Variable_Name can be # anything the user wants. Dynamic_Variable_Name = "geek" # The value 2020 is assigned # to "geek" variable locals()[Dynamic_Variable_Name] = 2020 # Display variable print(geek) Output: 2020Using exec() method to create dynamically named variables Here we are using the exec() method for creating dynamically named variable and later assigning it some value, then finally printing its value. Python3 # Dynamic_Variable_Name can be # anything the user wants. Dynamic_Variable_Name = "geek" # The value 2020 is assigned # to "geek" variable exec("%s = %d" % (Dynamic_Variable_Name, 2020)) # Display variable print(geek) Output: 2020Using vars() method to create dynamically named variables Here we are using the vars() method for creating dynamically named variable and later assigning it some value, then finally printing its value. Python3 # Dynamic_Variable_Name can be # anything the user wants. Dynamic_Variable_Name = "geek" # The value 2020 is assigned # to "geek" variable vars()[Dynamic_Variable_Name] = 2020 # Display variable print(geek) Output: 2020 Comment More infoAdvertise with us Next Article Create a File Path with Variables in Python P pulamolusaimohan Follow Improve Article Tags : Python Python Programs python-basics Practice Tags : python Similar Reads How to Use Words in a Text File as Variables in Python We are given a txt file and our task is to find out the way by which we can use word in the txt file as a variable in Python. In this article, we will see how we can use word inside a text file as a variable in Python. Example: Input: fruits.txt apple banana orange Output: apple = Fruit banana = Fru 3 min read Create a File Path with Variables in Python The task is to create a file path using variables in Python. Different methods we can use are string concatenation and os.path.join(), both of which allow us to build file paths dynamically and ensure compatibility across different platforms. For example, if you have a folder named Documents and a f 3 min read Write Multiple Variables to a File using Python Storing multiple variables in a file is a common task in programming, especially when dealing with data persistence or configuration settings. In this article, we will explore three different approaches to efficiently writing multiple variables in a file using Python. Below are the possible approach 2 min read Insert a Variable into a String - Python The goal here is to insert a variable into a string in Python. For example, if we have a variable containing the word "Hello" and another containing "World", we want to combine them into a single string like "Hello World". Let's explore the different methods to insert variables into strings effectiv 2 min read How to Add User Input To A Dictionary - Python The task of adding user input to a dictionary in Python involves taking dynamic data from the user and storing it in a dictionary as key-value pairs. Since dictionaries preserve the order of insertion, we can easily add new entries based on user input.For instance, if a user inputs "name" as the key 3 min read Changing Variable Names With Python For Loops When we write variable names play a crucial role in code readability and maintainability. However, there are situations where you might need to change variable names systematically, perhaps to adhere to a naming convention, improve clarity, or for any other reason. In Python, the flexibility of for 3 min read Like