-1

I want to create a variable as Variable11 and assign its vcalue to 10.

When using 'Variable'+ str(11) = 10 i get a error stating cant assign to operator

Is there any better way to create a concate string as variable to assign a value.

  • You probably should be using a `list` or a dictionary instead. – DenverCoder1 Apr 06 '20 at 11:35
  • Don't dynamically create variables, *use a container* like a list or a dict. – juanpa.arrivillaga Apr 06 '20 at 11:37
  • You can use globals() but that's not ideal. https://thepythonguru.com/python-builtin-functions/globals/ – Zabir Al Nazi Apr 06 '20 at 11:39
  • If you try and succeed in creating variables dynamically you have solved only first part of your problem. Second part of the problem will be 'how to dynamically access created variables'. As suggested by earlier comments - use proper datastructures built-in into Python. – Aivar Paalberg Apr 06 '20 at 11:46

2 Answers2

0

Do it like this :

new_var = old_var +"11"

Rajnish kumar
  • 186
  • 1
  • 14
-1

Use exec()

>>> exec(f'Variable{str(11)} = 10')
>>> Variable11
10
Badgy
  • 819
  • 4
  • 14