0

here is what I want to do:

b=1
har1=42
print(f"har{b}")

I want it to display 42

I have tried a few things, but I haven't managed to get it to work

1 Answers1

-1

Use either globals() or locals() and construct the key string.

b=1
har1=42
globals()['har'+str(b)]

def f():
    b=1
    har1=42
    print(locals()['har'+str(b)])

f()
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52