Strings use single quotes, only. There is no double-quote form — one
way to write a string, no style debates. Escape a quote with \':
1 2
msg = 'he said \'hi\''
log[msg]expected output — from the book, verified by crow test --book
he said 'hi'Any expression between paired backticks is evaluated into the string:
1 2 3 4
name = 'crow'
n = 4
log['hello, `name`']
log['`n` squared is `n * n`']expected output — from the book, verified by crow test --book
hello, crow
4 squared is 16Outside backticks, strings are completely inert — braces, JSON, and regex
punctuation need no escaping. For a literal backtick, write \` . A
backslash before any other character stays as it is, so a regular
expression is written as it is: '\d+', '\|'.
len[], slice[], and indexing all work in characters, never bytes, so
accented letters and dashes are one step each in both execution modes:
1 2 3 4 5
s = 'naïve — café'
log[len[s]]
log[slice[s, 6, 7]]
log[s[-1]]
log[s[2]]expected output — from the book, verified by crow test --book
12
—
é
ïoccurs counts how many times a part occurs, not overlapping (as Python's
str.count counts), and translate maps each character of one text to
the character at its place in another, all in one pass — the complement
of a strand of DNA:
1 2 3 4
dna = 'GATTACA'
log[occurs[dna, 'A']]
log[occurs['aaaa', 'aa']]
log[translate[dna, 'ACGT', 'TGCA']]expected output — from the book, verified by crow test --book
3
2
CTAATGT+ on strings+ is for numbers. To combine strings, interpolate; to build from many
pieces, collect and join:
1 2 3 4
parts = split['a,b,c', ',']
log[parts]
log[join[parts, '-']]
log[upper['crow']]expected output — from the book, verified by crow test --book
['a', 'b', 'c']
a-b-c
CROWs = 'a' + 'b' --- error: cannot use '+' with strings
The error message itself tells you the two idioms above. For value
formatting (decimals, widths, percents) there's fmt[...] with named
arguments — chapter 10 uses it heavily.