Session 5

String Variables

String variables are variables that contain strings rather than numbers. They behave largely the same as numeric variables: string values can be assigned with the assignment operator '=', assigned values can be literal strings, query results, or even formulas, and variables are replaced with the value at runtime. Basically, there's only one formal difference between numeric and string variables, i.e. the name of a string variable must end in a dollar symbol: string_var$. Apart from that the previously established naming conventions apply. Other differences are due to general string handling principles of the Praat scripting engine. You're already familiar with the most important principle: literal strings must be enclosed in double quotes. This statement assigns a literal string to a string variable:

my_string$ = "I'am a string."

Any literal string in a script can be replaced with a string variable—same as any literal number can be replaced with a numeric variable. An example:

obj_name$ = "silence" Create Sound from formula: obj_name$, 1, 0, 1, 44100, "0"

Don't use double quotes with string variables if you want the variable's name substituted with its value (usually that's exactly what you want). The dollar symbol is enough to tell the scripting engine that it is dealing with a string. Double quotes are only necessary as a marker for literal strings. Consequently, if a variable is enclosed in double quotes it is interpreted as a literal string. After running this script:

obj_name$ = "silence" Create Sound from formula: "obj_name$", 1, 0, 1, 44100, "0"

you end up with a new object called obj_name_ instead of silence, because the expression "obj_name$" was interpreted as literal string, not as the name of a variable. (Because Praat disapproves of object names containing dollar symbols, dollar is automatically replaced by underscore.)

Most query commands in the GUI return numeric values like sampling frequency, duration etc. But it is also possible to query for strings. For example, Get label of interval..., a command which is available if a TextGrid is selected, returns a string, namely the the label of a specific interval in a specific tier of the TextGrid (labels are always handled as strings). To query the first label of tier 1 in a script you would try something like this:

label$ = Get label of interval: 1, 1

Strings in formulas

As mentioned above, string variables are similar to numeric variables in that they accept literal values, queries and even formulas on the right side of the assignment operator. Now that we have discussed literal values and queries, you may wonder what formulas have to do with strings. Is it possible to add or subtract strings? Not quite, but it is certainly possible to concatenate and truncate strings.

part1$ = "I like " part2$ = "Praat" sentence1$ = part1$ + part2$ sentence2$ = sentence1$ - "Praat" + "icecream" writeInfo: sentence1$, newline$, sentence2$

The first two statements assign literal strings to string variables—nothing new there. The third statement combines the two strings and assigns the resulting string to a new string variable. The forth statement first truncates the combined string, getting rid of Praat, and 'adds' a new string to the remainder, assigning the result to a new variable. The last statement prints the two concatenated strings to Praat Info:

I like Praat
I like icecream

A more practical application is this: Suppose you have the filename of a sound file available in a variable. Than you can derivate the object name (in case you load the file) by truncating the filename suffix or you can construct a new filename, to save an associated TextGrid for instance.

filename$ = "recording.wav" # derivate object name objectname$ = filename$ - ".wav" # construct a new filename and save a TextGrid gridname$ = filename$ - ".wav" + ".TextGrid" Save as text file: gridname$

+ and - are the only string operators, but there are a lot of interesting built-in string functions which can also be used in string formulas. You already came across of fixed$ () in the last workshop and you'll come to know some more of them in Commands and Functions.

A remark on operators: As you may have noticed, the meaning of operators is context-sensitive. For instance, the operator + means addition in a numeric context but concatenate in a string context. If the context is ambiguous, i.e. if you try for example to add a number to a string ("2" + 2), Praat terminates with an error messages. Later you'll see that the operator = is also homonymous. In an assignment context it's the assignment operator but in other contexts (conditional expressions) it's a comparison operator.

Numbers vs. strings

Just to be clear about the add-number-to-string example ("2" + 2), let me point out once more the difference between a string and a number: A string is a sequence of arbitrary characters (letters, digits, punctuation marks, special symbols like underscore, spaces etc.) with no special meaning to Praat. A number on the other hand has an 'arithmetic' meaning, numbers can be calculated. Proper numbers are sequences of digits. Whole numbers (numbers without decimal point) are called integer numbers, numbers with a decimal point are called real numbers. Positive integers are called natural numbers, positive real numbers are called positive numbers. All types of numbers can be used in Praat and assigned to numeric variables. There's no comparable typology for strings. Each string is just an unstructured accumulation of characters. This is the reason why "2" and 2 are two completely different things. 2 can partake in an arithmetic calculation, "2" can't partake because it's just some character without special meaning.

A little quiz: What is the content of a and what is the content a$ after the following assignments:

a = 2 + 2 a$ = "2" + "2"
  • Answer

    a contains the number 4 (result of an addition) and a$ contains the string 22 (result of a concatenation)

The numeric world and the string world are strictly divided. But sometimes you need to transfer something between the worlds, i.e. you need a string (exclusively composed of digits) to behave like a number or you need a number (any number) to behave like a string. Fortunately, Praat provides built-in functions for conversions (details in session 7). string$ () converts a number to a string, number () converts a string to a number. To assign the number 4 to a you can do:

a = number ("2") + number ("2")

And to assign the string 22 to a$ you can do:

a$ = string$ (2) + string$ (2)


Variables are containers for dynamic content. Praat provides two types of variables: numeric and string variables. Variable names must be unique and comply with the naming conventions (initial small case letter optionally followed by letters, digits, and underscore). Names of string variables must end in a dollar symbol.

Values are assigned to variables in assignment statements using the assignment operator. Besides literal values, it's possible to assign query results and formula results.

You may replace any literal number or string in a script with a variable of the respective type. At runtime, variables are substituted with their value. Variable names enclosed in double quotes are not interpreted as variables but as literal strings (i.e. they are not substituted).

© Jörg Mayer

Last modified: