How do I cast a string to a symbol?

By applying the $ (cast) operator while passing ` (the null symbol) as its left argument:

q)` $ “foo”

The cast operator also accepts an uppercase type character as its left argument:

q)”S” $ “foo”

Moreover, you can convert a list of strings at once:

q)”S” $ (“foo”; “bar”; “baz”)

To go the other way, use the string function:

q)string `foo

Since the string function is implemented as the monadic form (i.e., single argument overload) of the k function $


— it is also possible to write the above cast as follows:


Although we don’t recommend it, we thought you should know in case you see it in the wild.

See also: type

How do I build a ctrl-A delimited string?

Short answer: “\001” sv (“one”; “two”; “three”)

To build delimited strings in general, use the sv (scalar from vector) function:

q)”, ” sv (“Hello”; “world!”)
“Hello, world!”

As in many programming languages, the syntax for non-printable ASCII characters is borrowed from C, although only a subset of C’s sequences is supported:

\n      newline
\r      carriage return
\t      tab

(q also supports a few printable escape sequences: \\, \” and \’). However, as in C, you can express any ASCII code in q by following a backslash with the code’s 3 digit octal representation:

q)”\054\040″ sv (“Hello”; “world!”)
“Hello, world!”

Since ctrl-A is ASCII 1, we need to use \001:

q)fstring: “\001” sv (“f1=va”; “f2=vb”; “f3=vc”)

We can use 0: to write fstring to a file, and use an external program to confirm the result:

q)`:test 0: enlist fstring
$ hexdump test
0000000 66 31 3d 76 61 01 66 32 3d 76 62 01 66 33 3d 76
0000010 63 0a

To break fstring up into its parts, see How do I parse a ctrl-A delimited string?

How do I declare a table column of type string (not symbol!)?

Declare it without a type as follows:

q)t: ([] str: ())
q)meta t
c  | t f a
---| -----

A the type of a column defined as () is determined when the first row is inserted:

q)`t insert enlist enlist "foo"

This work is licensed under a Creative Commons License.
The views and opinions expressed herein are those of the authors and do not necessarily reflect those of any other person or legal entity.
Kdb+ is the registered trademark of Kx Systems, Inc.