adding string to a var name

441 views python

I would like to add a string to my var names, they look like following:

bcoal = solph.Bus()
blig = solph.Bus()
bgas = solph.Bus()
bbio = solph.Bus()
bel = solph.Bus()

I want to create these vars more than 1 time, for some areas. So I want to add some string like _area1, which will basically change the var names to:

bcoal --> bcoal_area1

How can I do that?

Script should be look like this:

def create(name):
   bcoal+str(name) = solph.Bus()
   # etc...

so that I can create area1 via area1 = create(area1)

answered question

Are you talking about variable name?

1 Answer


Changing variable names dynamically like that is generally a bad idea. Instead you can store the objects in a dictionary:

d = {
    'bcoal': solph.Bus(),
    'blig': solph.Bus(),
    'bgas': solph.Bus(),
    'bbio': solph.Bus(),
    'bel': solph.Bus(),

You can access them as follows:


and you can update the names:

d = {k + '_area1': v for k, v in d.items()}

posted this

Have an answer?


Please login first before posting an answer.