# 6.2 Strings

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:

| `’Robert’s data’`

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

| `’’’Robert’s data’’’`

Special characters such as tabs and newlines can be inserted into strings using escape codes such as `t` and `n`; see Table 6.1 for a list of these. The following string is split over three lines:

`pyxplot> print e’the moon,nthe moon,nThey danced by the light of the moon.’`
`the moon,`
`the moon,`
`They danced by the light of the moon.`

Sometimes these escape codes can be rather annoying, especially when entering latex control codes, which all begin with backslash characters. Rather than having to escape every backslash, it is generally easier to prefix the string with the character `r`, which turns off all escape codes:

`pyxplot> print r”’I escaped the quote by typing ’.”’`
`I escaped the quote by typing ’.`

Escape sequence | Description
`?` | Question mark
`’` | Apostrophe
`"` | Double quote
`` | Literal backslash
`a` | Bell character
`b` | Backspace
`f` | Formfeed
`n` | Newline
`r` | Carriage return
`t` | Horizontal tab
`v` | Vertical tab

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

Once defined, a string variable can be used anywhere in Pyxplot where a quoted string could have been used, for example in the `set title` command:

    plotname = "Insert title here"
    set title plotname

Strings can be concatenated together using the `+` operator:

`pyxplot> print "pi = " + pi.str()`
`pi = 3.1415927`

- 6.2.1 The string substitution operator
- 6.2.2 Converting strings to numbers
- 6.2.3 Slicing strings
- 6.2.4 String methods
- 6.2.5 Regular expressions