-4

For example

i='a'  
b='b'  
i+b='dog'  
print (ab)

Expected result: 'dog'
received result: 'Error: can't assign to operator"

Froog
  • 31
  • 2

2 Answers2

1

Though it's not recommended to do, you can update globals:

i='a'  
b='b'  
globals()[i+b] = 'dog'
print (ab) # 'dog'

Another (not recommended) way of achieving the same result is using exec:

exec "ab = 'dog'"
print ab

A better way to achieve dynamically changing key-value pairs can be achieved by using a dictionary:

mapper = {}
mapper[i+b] = 'dog'
print mapper['ab'] # 'dog'
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
-1
>>> i='a'
>>> b='b'
>>> locals()[i+b] = 'dog'
>>> print(ab)
dog

I ... uhh ... hope this helps.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87