6.2.1 The string substitution operator

Most string manipulations are performed using the string substitution operator, %, which performs a similar role to the sprintf statement in C.

This operator should be preceded by a format string, such as ’x=%f’, in which tokens such as %f mark places where numbers and strings should be substituted. The substitution operator is followed by a bracketed list of the quantities which should be substituted in place of these tokens. This behaviour is similar to that of the Python programming language’s % operator1

For example, to concatenate the two strings contained in the variables a and b into a single string variable c, one would issue the command:

c = "%s%s"%(a,b)

One application of this operator might be to label plots with the title of the data file being plotted, as in the following example:

filename="data_file.dat"
title=r"A plot of the data in {\tt %s}."%filename
set title title
plot filename

The syntax of the substitution tokens placed in the format string is similar to that used by many other languages (including C and Python). All substitution tokens begin with a % character, after which there may be placed, in order:

  1. An optional minus sign, to specify that the substituted item should be left-justified.

  2. An optional integer specifying the minimum character width of the substituted item, or a * (see below).

  3. An optional decimal point/period (.) separator.

  4. An optional integer, or a * (see below), specifying either (a) the maximum number of characters to be printed from a string, or (b) the number of decimal places of a floating-point number to be displayed, or (c) the minimum number of digits of an integer to be displayed, padded to the left with zeros.

  5. A conversion character.

The conversion character is a single character which specifies what kind of substitution should take place. Its possible values are listed in Table 6.2.

Character

Substitutes

d, i

An integer value.

e, E

A floating-point value in scientific notation using either the character e or E to indicate exponentiation.

f

A floating-point value without the use of scientific notation.

g, G

A floating-point value, either using scientific notation, if the exponent is greater than the precision or less than $-4$, otherwise without the use of scientific notation.

o

An integer value in octal (base 8).

s, S, c

A string, if a string is provided, or a numerical quantity, with units, if such is provided.

x, X

An integer value in hexadecimal (base 16).

%

A literal % sign.

Table 6.2: The conversion characters recognised by the string substitution operator, %.

Where the character * is specified for either the character width or the precision of the substitution token, an integer is read from the list of items to be substituted, as happens in C’s printf command:

pyxplot> print "%.*f"%(3,pi)
3.142
pyxplot> print "%.*f"%(6,pi)
3.141593

Footnotes

  1. As in Python, the brackets are optional when only one item is being substituted. For example, ’%d’%2 is equivalent to ’%d’%(2).