168

I have any string. like 'buffalo',

x='buffalo'

I want to convert this string to some variable name like,

buffalo=4 

not only this example, I want to convert any input string to some variable name. How should I do that (in python)?

2
  • 29
    Why would you want to do that? You generally use a dictionary if you need to map strings to values. Commented Oct 1, 2013 at 17:29
  • None of the answers so far handle common error cases, such as having a space or a period in the string. Commented Dec 2, 2020 at 18:30

3 Answers 3

184
x='buffalo'    
exec("%s = %d" % (x,2))

After that you can check it by:

print buffalo

As an output you will see: 2

Sign up to request clarification or add additional context in comments.

9 Comments

Even in the very rare cases where you do have a good reason to dynamically create a variable, this is not the way to do it; just use setattr. And even when setattr is inappropriate for whatever reason, being explicit and modifying locals or globals as appropriate is still better than exec.
I just showed the solution that does exactly the same as OP request.
+1 since it's what the OP wanted even though it's very unsafe and shouldn't user be used.
exec() is not dangerous: what is dangerous is the on how and in which context you use it.
@BillalBEGUERADJ Thank you for adding some sense to the conversation. People always blindly freak out about exec/eval that they never understand how powerful and useful it can be when used correctly.
|
47

This is the best way, I know of to create dynamic variables in python.

my_dict = {}
x = "Buffalo"
my_dict[x] = 4

I found a similar, but not the same question here Creating dynamically named variables from user input

3 Comments

You need to explain more how this solves the OPs problem, and most of all, refer to x, not the string literal.
I don't get it this solution. Does my_dict["Buffalo"] prints 4? The proper answer should be print(Buffalo) should be 4 such as print(x), rather than print(my_dict["Buffalo"]).
I have used this in another script. Strangely, my_dict[x] = my_dic[x] + n is working while my_dict[x] += n is not working. Why does this happen?
24

You can use a Dictionary to keep track of the keys and values.

For instance...

dictOfStuff = {} ##Make a Dictionary

x = "Buffalo" ##OR it can equal the input of something, up to you.

dictOfStuff[x] = 4 ##Get the dict spot that has the same key ("name") as what X is equal to. In this case "Buffalo". and set it to 4. Or you can set it to  what ever you like

print(dictOfStuff[x]) ##print out the value of the spot in the dict that same key ("name") as the dictionary.

A dictionary is very similar to a real life dictionary. You have a word and you have a definition. You can look up the word and get the definition. So in this case, you have the word "Buffalo" and it's definition is 4. It can work with any other word and definition. Just make sure you put them into the dictionary first.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.