5

I am very new with python and programming altogether and have been trying to figure out how to do this for a while.

Here's what I need help with:

y=0
x=2
p01='hello'
p02='bye'

print('p'+(str(y)+str(x)))

The output is of course 'p02', but how can I make the output instead the value of p02 ('bye')

Hope this makes sense and I look forward to any answers.

Jakey Snakey
  • 95
  • 1
  • 1
  • 7
  • 3
    There are ways to do this, but the correct way to approach this is to use a `dict`. Is there a reason you are not using a `dict`? For example, `d = {'p01': 'hello', 'p02': 'bye'}; print(d['p'+(str(y)+str(x))])` – SethMMorton Jul 30 '17 at 05:55
  • Sounds like an XY problem. Why do you need this anyway? Rethink and restructure. – Andrew Li Jul 30 '17 at 05:59

1 Answers1

6

You could use eval()...

It evaluates an expression stored in a string as if it were Python code.

In your case, 'p'+(str(y)+str(x)) becomes 'p01', so it gets the result of the expression p01, which is of course 'bye'.

print(eval('p'+(str(y)+str(x))))

Note however that you should never do this - there is almost always a better way. Please read Why is using 'eval' a bad practice?

So, what can we do?

globals() gives us a dictionary of all of the global variables in your Python program, with their name as a string index and their value as the dictionary value. Thus, we can simply do:

globals()['p'+(str(y)+str(x))]

Which evaluates to globals()['p01'], which gets the value of global p01 - which is bye.

Again, this is a workaround to a bigger problem

Restructure your code. Make them into an array or dictionary and get the index of it. Think through why you would want to do this, and change your code so that you do not have to. It is bad to be in a situation where eval looks like the best option.

Toastrackenigma
  • 7,604
  • 4
  • 45
  • 55
  • 1
    To whomever downvoted... please explain so the answer can be improved. Yes, "`eval` is evil" and if that is why you downvoted you should explain why.. – SethMMorton Jul 30 '17 at 05:57
  • 1
    If the OP refuses to use their own dict, a better way to do this would be to do `globals()['p'+(str(y)+str(x))]` (or `locals()['p'+(str(y)+str(x))]` if inside a function). This would eliminate the use of `eval`. – SethMMorton Jul 30 '17 at 06:02
  • 1
    And while we are at it, this is much clearer... `'p{}{}'.format(y, x)`. – SethMMorton Jul 30 '17 at 06:03
  • @SethMMorton Thanks for that! I was trying to come up with a way of doing this so I could say *you could use eval, but this better*, but now I can use yours :D I did not know about `globals()`. – Toastrackenigma Jul 30 '17 at 06:05
  • 1
    If you want to suggest something better, suggest using a `dict` in the first place. – SethMMorton Jul 30 '17 at 06:07
  • @JakeySnakey Awesome, glad we could help! If this is solved, accept it (by clicking the checkmark next to it) so as to help people looking for a solution to this problem in the future. – Toastrackenigma Jul 30 '17 at 06:15