3.5 Printing text

Pyxplot’s print command can be used to display strings and the results of calculations, as in the following examples:

pyxplot> a=2
pyxplot> print "Why don’t fish get lost in the ocean?"
Why don’t fish get lost in the ocean?
pyxplot> print a
2

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
pyxplot> a=3
pyxplot> print "The value of ",a," squared is ",f(a),"."
The value of 3 squared is 9.

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:

\includegraphics[width=0.9cm]{cross.eps}

’Robert’s data’

This easiest way to avoid such problems is to use three quotes:

\includegraphics[width=0.9cm]{tick.eps}

’’’Robert’s data’’’

Alternatively, the $\backslash $ character may be used to escape quote characters. Two backslashes characters – $\backslash \backslash $ – produce a literal backslash:

\includegraphics[width=0.9cm]{tick.eps}

’Robert$\backslash $’s data’
"I typed $\backslash \backslash $’ to get an apostrophe"

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 $\backslash $, $\backslash $" and $\backslash \backslash $.

The use of other escape characters may be enabled by prefixing a string with the character e, as in the example e’$\backslash $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,$\backslash $nthe moon,$\backslash $nThey danced by the light of the moon.’
the moon,
the moon,
They 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 $\backslash $’.”’
I escaped the quote by typing $\backslash $’.

Escape sequence

Description

$\backslash $?

Question mark

$\backslash $

Apostrophe

$\backslash $"

Double quote

$\backslash \backslash $

Literal backslash

$\backslash $a

Bell character

$\backslash $b

Backspace

$\backslash $f

Formfeed

$\backslash $n

Newline

$\backslash $r

Carriage return

$\backslash $t

Horizontal tab

$\backslash $v

Vertical tab

Table 3.2: A complete list of Pyxplot’s string escape sequences. These are a subset of those available in C.

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
pyxplot> print "The value of %d squared is %d."%(a,f(a))
The value of 3 squared is 9.
pyxplot> print "The %s of f(%f) is %d."%("value",sqrt(2), $\backslash $
.......>                                 f(sqrt(2))     )
The value of f(1.414214) is 2.

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.