Skip to content

10 · Strings

Literals and Escapes

Double quotes, escape sequences (\n \t \" \\) and the char type.

On this page

Every string in Zolo uses double quotes. There is no single-quote string syntax — 'a' is a character literal (type char), useful in comparisons but distinct from str.

The basic escape sequences are \n (newline), \t (tab), \" (quote inside a string), and \\ (literal backslash):

Literals, escapes, and the .len() method on simple strings.

01-literals-and-escapes.zolo
Playground
// Feature: Strings — literals and escape sequences

// Syntax: `"..."`, `"""..."""`, and raw `r"..."` strings

// When to use: represent text. Single quotes are CHAR literals.


// Strings always use double quotes.

let greeting = "Hello, world!"
print(greeting)

// expected: Hello, world!


// Basic escape sequences.

print("line 1\nline 2")  // \n line break

print("col1\tcol2\tcol3")  // \t tab

print("quote: \"")  // \" literal quote inside string
print("slash: \\")  // \\ literal backslash

print("return: end\rstart")  // \r carriage return


// Single quotes are CHAR literals (1 character only).

// Useful for character comparisons, not strings.

let c = 'a'
print(c == 'a')

// expected: true


// Cooked strings and character literals accept Unicode scalar escapes.

print("Açaí - café")
print("emoji: \u{1F680}")
print('\u{03BB}')

// Empty strings and single-char strings.

let empty = ""
let s = "x"
print(empty.len())  // 0

print(s.len())  // 1

Challenge

Create a string containing a backslash followed by a literal n (i.e. the two characters \ and n, not a newline). Print it and confirm the output shows \n and not a blank line.

Exact byte values

b"\xAB" stores one byte with value 171. Each hexadecimal escape requires exactly two digits. Unescaped text contributes UTF-8 bytes, while escapes preserve zero and invalid UTF-8 bytes. Byte literals do not interpolate expressions. .len() counts bytes; .bytes() returns a copy as integer values, indexed from zero like other arrays.

10-byte-literals.zolo
Playground
// The KTX 2 signature contains bytes that are not valid UTF-8.

let signature = b"\xABKTX 20\xBB\r\n\x1A\n"
print(signature.len())
print(signature.bytes()[0])
print(signature.bytes()[7])

Byte literals run on the VM, native and LLVM backends, including the browser VM. Standalone wasm-aot currently rejects them at compile time because it has no opaque bytes representation.

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br