I don't have much experience with Python but this is relatively simple in Ruby (the solution could probably be translated for anyone more familiar with python!):

Code:
original_string = "{VAR123} blah blah {VAR45} bla blaaa {VAR54212}"
modified_string = original_string.gsub(/\{VAR([0-9]+)\}/).with_index do |m, i|
  %{<img src="cid:image#{i+1}">}
end
puts modified_string
 => <img src="cid:image1"> blah blah <img src="cid:image2"> bla blaaa <img src="cid:image3">