3.5 Printing text
Multiple items can be displayed one-after-another on a single line by separating them with commas. The following example displays the values of the variable a and the function f(a) in the middle of a text string:
pyxplot> f(x) = x**2
Strings can be enclosed either in single (’) or double (") quotes. Strings may also be enclosed by three quote characters in a row: either ’’’ or """. Special care needs to be taken when using apostrophes or quotes in single-quote delimited strings, as these characters may be misinterpreted as string delimiters, as in the example:
This easiest way to avoid such problems is to use three quotes:
Alternatively, the character may be used to escape quote characters. Two backslashes characters – – produce a literal backslash:
Special characters such as tabs and newlines can be inserted into strings using escape codes, but these are disabled by default. This is because latex uses the backslash as its own escape character, and strings in Pyxplot are commonly used to contain latex commands. So, by default the only escape characters that Pyxplot expands are ’, " and .
The use of other escape characters may be enabled by prefixing a string with the character e, as in the example e’t’ for a tab character. See Table 3.2 for a complete list of escape codes available. For example, the following string is split over three lines:
pyxplot> print e’the moon,nthe moon,nThey danced by the light of the moon.’
Alternatively, strings may be prefixed with the character r to turn off all escape codes, including for quote characters:
pyxplot> print r”’I escaped the quote by typing ’.”’
When many items are being printed together on a line, they can be concatenated together using the + operator as above, but it is usually neater to use the string substitution operator, %. The operator is preceded by a format string, in which the places where numbers and strings are to be substituted are marked by tokens such as %e and %s.
The substitution operator is followed by a ()-bracketed list of the quantities which are to be substituted into the format string. This behaviour is similar to that of python’s % operator, and of the printf command in C, as the following examples demonstrate:
pyxplot> f(x)=x**2 ; a=3
The detailed behaviour of the string substitution operator, and a full list of the substitution tokens which it accepts, are given in Section 6.2.1.