0

How can I create variable with name from result of input?

Example:

username = input()
result of username is example for John

Now I need to create variable with name John a content will be empty string.

John = ""
J. Doe
  • 1
  • you dont. Thats not how variable names work. However you could create a dictionary: `myDictionary = { username : ""}` - see [dictionarys](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) thats stores your empty string under a key `"John"` – Patrick Artner Jul 07 '18 at 16:40
  • So, if I need to create automatically so many variables of any names, it is not possible, is not it? – J. Doe Jul 07 '18 at 16:42
  • Maybe you should follow a python tutorial that explains lists and data. If you need the user to input 10 names, you could store them in a list like so: `mylistofnames = []` which creates an empty list and then you repeat the input() 10 times: `for _ in range(10):` + `mylistofnames.append(input())` ( you need to indent the stuff that should be looped by 4 spaces. See [for-loop-statement](https://docs.python.org/3/tutorial/controlflow.html#for-statements) and [list's](https://docs.python.org/3/tutorial/introduction.html#lists) – Patrick Artner Jul 07 '18 at 16:47
  • @J.Doe If you insist, it certainly is possible. Just set the dict returned by the built-in functions `globals()`. For example, `globals()['John'] = ''` is equivalent to `John = ''`. – blhsing Jul 07 '18 at 16:54
  • @blhsing Can you post the code please? – J. Doe Jul 07 '18 at 16:55
  • @J.Doe I already did. `globals()['John'] = ''`.. or as your question requests, `globals()[input()] = ''`. – blhsing Jul 07 '18 at 16:56
  • @blhsing theres advice and advice. yours ... is bad. – Patrick Artner Jul 07 '18 at 16:57
  • @PatrickArtner I did put a disclaimer of "if you insist". – blhsing Jul 07 '18 at 16:58

0 Answers0