0

can I use a variable's value as a new variable? How I can do that?

a = "rajiv"

I want to set rajiv to another value a = "1"

but I get a's value as 1 and not rajiv's

RajivKumar
  • 65
  • 8

2 Answers2

0

You could use globals()

>>> a = 'rajiv'
>>> globals()[a] = 1
>>> rajiv
1
JoseKilo
  • 2,343
  • 1
  • 16
  • 28
0

You can use globals() like the others have suggested.
However, it is thought to be bad practice
I say utilize object in python

obj = {}
obj['a'] = 'foobar'
obj[obj['a']] = 'hello'
print(obj) # {'a': 'foobar', 'foobar': 'hello world'}
Community
  • 1
  • 1
taesu
  • 4,482
  • 4
  • 23
  • 41