Strings are represented with single quotes in Gekko (for instance: ‘this is a string’). Some languages use double quotes, and other languages allow both variants (or use single quotes for characters, and double quotes for strings). The use of single quotes for strings in Gekko has at least one convenience: double quotes are allowed inside a Gekko strings, and will be treated just like any other character (this is sometimes practical).

Anyway, this post is not about single quotes versus double quotes. It is about strings inside strings, or how strings can be used to compose other strings. The strict way of doing this is, for instance, %c = ‘red’; %s = ‘a ‘ + %c + ‘ car’;. This will be the same as ‘a red car’, but using the ‘+’ for chopping up a string is not very readable, especially if there are many inserts like %c. In Gekko versions prior to 3.0, it was possible to use %s = ‘a %c car’ instead. Allowing such syntax is of course tempting, and it is even one of the reasons sigils (type symbols like ‘%’) are used in some computer languages. But as the case often is: the devil is in the details. What if %c is not surrounded by blanks? It may be surrounded by ‘%’, ‘|’, ‘{‘, ‘}’, and so on, and it is not always easy to be sure that this works as intended in all corner cases.

Therefore, it was decided to use string interpolation instead, where string interpolation means inserting a string inside another string by means of certain placeholders, in practice implementing something akin to the f-string concept known from Python. So in Gekko 3.0, you should use %s = ‘a {%c} car’, where the {}-curlies is the placeholder. In Gekko 3.0, any expression may be put inside these {}-curlies, as long as the inside evaluates to a string or value. Therefore, the following is possible: %c1 = ‘r’; %c2 = ‘ed’; %s = ‘a {%c1 + %c2} car’;, where the inside of the {}-curly evaluates to ‘red’. Of course, ‘a {%c1}{%c2} car’ could be used instead, so the example is just to illustrate that expressions are allowed. Another example could be %t = 2020; TELL ‘The {%t} value is: {y[%t]/1000}’;, where y is a timeseries. You may even use {}-curlies inside {}-curlies, for instance: %s = ‘a{‘x{‘y’}z’}c’; which is that same as %s = ‘axyzc’;.

This is all very good, but the best part of it is probably that names and strings in Gekko 3.0 are very easy to convert from one to another. For instance, in Gekko 3.0, x{%i}z is a variable name, and if %i = ‘y’, the name is xyz. If you need to convert this name into a string, you just add the single quotes: ‘x{%i}z’, which is the same as ‘xyz’. Conversely, the string ‘x{%i}z’ can be converted into x{%i}z by simply removing the quotes. In practice, this conversion arises quite often, for instance because a command or function only accepts a string rather than a name. An example could be a command like y = y + x{%i}z; where it is not certain if x{%i}z exists (and if it does not, y should be unchanged). There is an exist() function that checks for just that, but this function only accepts strings. So the syntax becomes IF (exist(‘x{%i}y’) == 1); y = y + x{%i}z; END; which easy to understand. Another example is that a list of strings like #m = (‘a{%i}b’, ‘x{%i}y’) can alternatively be written as a naked list, in the form #m = a{%i}b, x{%i}y, where the parentheses and quotes are dropped.

All in all, using string interpolation with {}-curlies inside strings is convenient and consistent, and as an extra convenience, the same kind of syntax is used regarding name-substitution, that is, {}-curlies inside variable names. You can even tell Gekko to format the {}-curlies in particular ways, for instance number of decimals (if the inside is a value), cf. OPTION string interpolate format.