6.2.1 The string substitution operatorMost 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:
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.
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) Footnotes
|