Skip to main content
Version: Python

Work with strings

Strings are sequences of characters that form words, sentences, phrases, or statements in programming languages. Deephaven queries use strings ubiquitously. Understanding their uses is critical to becoming a strong Deephaven developer.

note

This guide assumes you are familiar with using strings in Python. If not, please refer to the Python documentation for more information.

Strings in table operations (query strings)

The first and foremost place strings get used in Deephaven queries is table operations. The strings used as inputs for table operations are typically called query strings. A query string contains a formula, which either assigns data to or filters a column by relating the column to its values through the use of one or more operators.

The following code passes the query string X = i to update.

from deephaven import empty_table

source = empty_table(5).update("X = i")

X = i is a query string because it relates a column (X) to its values (i) by the assignment operator =. Query strings either create, modify, or filter data from tables. The following query creates data and then filters it with query strings.

from deephaven import empty_table

source = empty_table(10).update("X = i").where("X % 2 == 1")

Methods like update and where can take a list as input to pass in multiple query strings. The code below uses update to add two new columns, X and Y, to an empty table.

from deephaven import empty_table

source = empty_table(10).update(["X = i", "Y = String.valueOf(X)"])

Query strings and f-strings

Python's f-strings can be used to generate query strings in queries programmatically. This approach can increase the readability of queries by dramatically reducing the amount of code required. The query below creates two identical tables with 10 columns both with and without f-strings to show the difference in the amount of code required.

from deephaven import empty_table

source_fstring = empty_table(10).update([f"X{idx} = {idx} * i" for idx in range(10)])

source = empty_table(10).update(
[
"X0 = 0 * i",
"X1 = 1 * i",
"X2 = 2 * i",
"X3 = 3 * i",
"X4 = 4 * i",
"X5 = 5 * i",
"X6 = 6 * i",
"X7 = 7 * i",
"X8 = 8 * i",
"X9 = 9 * i",
]
)

Strings in query strings

Formulas in query strings can contain strings of their own. Strings inside query strings are denoted by backticks (`). The following code uses a query string that contains another string in the where operation.

from deephaven import new_table
from deephaven.column import string_col

source = new_table([string_col("X", ["A", "B", "B", "C", "B", "A", "B", "B", "C"])])

result = source.where(filters=["X = `C`"])

String literals in query strings

Formulas in query strings can also make use of string literals. These are denoted by single quotes ('). String literals are different from normal strings in query strings because they get interpreted differently. String literals can be inferred as another data type. The following example uses both a string and a string literal in query strings to show how they differ. The string literal gets inferred as a Duration to add one second to the timestamp.

from deephaven import empty_table

source = empty_table(1).update(["Now = now()"])

result = source.update(
["NowPlusString = Now + `PT1s`", "NowPlusStringLiteral = Now + 'PT1s'"]
)

Strings in tables

In the following example, a new table is created with two string columns.

note

String columns can be created using single or double quotes. Double quotes are recommended, especially when using string literals.

from deephaven import new_table
from deephaven.column import string_col

result = new_table(
[
string_col("X", ["A", "B", "B", "C", "B", "A", "B", "B", "C"]),
string_col("Y", ["M", "M", "N", "N", "O", "O", "P", "P", "P"]),
]
)

String concatenation

String columns can be concatenated in queries. The following example adds two [string columns] together with +.

from deephaven import new_table
from deephaven.column import string_col

source = new_table(
[
string_col("X", ["A", "B", "B", "C", "B", "A", "B", "B", "C"]),
string_col("Y", ["M", "M", "N", "N", "O", "O", "P", "P", "P"]),
]
)

result = source.update("Add = X + Y")

Escape characters

Strings in Deephaven, like Python, support the escape character \. The escape character invokes alternative interpretations of the characters that follow it. For example, \n is a new line and not the character n. Similarly, \t is a tab and not the character t.

The query below shows how Deephaven responds to these characters.

from deephaven import new_table
from deephaven.column import string_col

result = new_table(
[
string_col(
"X",
[
'Quote " in quotes',
"Single quote ' in single quotes",
"Escaped slash \\",
"New\nline",
"Added\ttab",
],
)
]
)
note

For more information on escaping characters in strings, see the Python documentation.

String filters

Deephaven supports using java.lang.String methods on strings in queries.

The following example shows how to filter a string column for values that start with "C".

from deephaven import new_table
from deephaven.column import string_col

source = new_table(
[string_col("X", ["Aa", "Ba", "Bb", "Ca", "Bc", "Ab", "Bd", "Be", "Cb"])]
)

result = source.where(filters=["X.startsWith(`C`)"])