Praat scripting tutorial

Introduction to Strings

The previous section talked about the concept of variables, and used number variables as a starting point. However, sometimes we want to manipulate more than numbers.

Let's remember how we assigned a number to a variable:

age = 34

This is something slightly different:

name$ = "Dan"

Two important details here: The variable name now ends in a dollar sign, and the value we assigned it is surrounded in double quotes (must be double! The single quotes mean something else...). This is how we indicate in Praat's language that instead of working with numeric data, we are working with "strings".

Hold on, what's a string?

A string is basically any character or combination of characters.

Why do I care? Why is there a difference between numbers and strings?

The main reason is because they take up different amounts of computer memory. Let's get nostalgic. A computer like the original Macintosh had only 128 kilobytes of memory (RAM)!!!!! That means that all computations the computer made had to take up less than this amount of space (like showing you all of the windows, processing clicks, making things appear on screen). At the time of writing, you can now attach a single file to an email that is up to 25Mb, which is 25000 kilobytes, about 200 times bigger than all of the computational space available to the first Mac! A string will take up more space in your memory than something like a number, so the computer needs to know how much space to set aside for a variable that will contain a string. Some of the important computer languages that we use today predate that old Macintosh, and these older computer languages require that the programmer choose what data type a variable would be to allow him or her to maximize to an extreme the available computational space. This focus on fine-grained control of memory still exists for anyone who's making a program that requires intensive computation and must be fast (like game programmers, for example). I don't want to go on a tirade about numbers of bits and 0's and 1's, so I leave it to the more curious to Google.

For example, the languages Praat is written in (C and C++) are both older than the Mac, and they have several further subdivisions of types of numbers (like an integer, real, long, or double), all of which take up different amounts of space. This is all to say that instead of being so cranky and aggressive, imaginary interlocutor, you should thank the creators of Praat for simplifying it down to two for us, because they are doing a lot of work behind the scenes to hide all of this complexity.

But I still don't care. Why should I care?

You should care because Praat cares: If the program is expecting a string and you give it a number or vice versa, you will get an error and your script won't work. So there. Every time you type anything in a Praat script, or ask Praat to give you a value, you have to think about its data type (number or string).

See the next section for some common string operations and some gotchas.

Note: What if I want double-quotes in my string?

Type this in and run it:

sentence$ = "Dan said "No way""
appendInfoLine: sentence$

You should have seen that this generates an error (our syntax coloring also served as a warning). The problem is that double-quotes have special significance for Praat: they enclose strings. To "escape" its special use, you have to type double-quotes twice:

sentence$ = "Dan said ""No way"""
appendInfoLine: sentence$

Next page: Strings: continued

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.