Skip to main content
1 of 3
Steven Kay
  • 20.6k
  • 5
  • 34
  • 82

what you're describing is called ‘string interpolation’.

In days of old, you could simply add strings together with the + operator, but this looks a bit ugly.

As you're using Python 3.6, the easiest way is called “f-strings”

I admit I’ve not tried this, but something like this should do the trick. Put an ‘f’ before the string, and put the variable name in {curly_brackets}

crop = [maize, wheat, rice]
for crop in crops:
    processing.run("saga:cliprasterwithpolygon", { 'INPUT' : f’/Users/.../{crop}_HarvestedAreaHectares.tif', 
        'OUTPUT' : f’/Users/.../{crop}.sdat', 
        'POLYGONS' : '/Users/.../africa_countries.shp' })
Steven Kay
  • 20.6k
  • 5
  • 34
  • 82