String Literals and Escapes
Write and escape text safely.
String Literals and Escapes is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Double-Quoted Literals
You write a string literal between double quotes. Zig stores its bytes in read-only memory and gives you a []const u8 slice pointing at them.
const msg = "Hello, Zig!";Escape Sequences
Inside a literal, a backslash starts an escape sequence. It lets you include characters that would otherwise be hard or impossible to type.
Newline and Tab
The classics still apply: \n inserts a newline and \t inserts a tab. They become single bytes, 10 and 9, inside your string.
const line = "name:\tZig\n";Escaping the Quote
To put a double quote inside text, escape it with \". Otherwise Zig would think the string ended early at that quote.
const q = "She said \"hi\".";Escaping the Backslash
Since backslash is special, you write a literal one as \\. This matters a lot for Windows paths full of backslashes.
const p = "C:\\dev\\zig";Hex Byte Escapes
Use \xNN to embed an exact byte by its two-digit hex value. It is handy for control bytes or binary data inside a literal.
const tab = "\x09"; // same as \tUnicode Escapes
Write any code point with \u{...} using hex digits in braces. Zig encodes it as the right UTF-8 bytes for you automatically.
const heart = "\u{2764}";Multiline Strings
For long text, start each line with \\. These multiline literals keep newlines and need no escaping for quotes or backslashes.
const sql =
\\SELECT *
\\FROM users
;No Trailing Newline
A multiline literal does not add a newline after the final line. You stay in full control of exactly which bytes end up in the string.
Literals Are Comptime
String literals are known at compile time, so Zig can fold and check them before your program ever runs. Their length is a constant.
Concatenate with ++
Join two compile-time strings with the ++ operator. The result is a new literal, also fixed and known at compile time.
const full = "Hello, " ++ "Zig!";Quick Check
You need a literal backslash inside a normal double-quoted string.
Recap
You can now build literals with escapes like \n, \", \xNN, and \u{...}, use \\ multiline blocks, and join text with ++. ✨
Frequently asked questions
Is the “String Literals and Escapes” lesson free?
Yes — the full text of “String Literals and Escapes” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “String Literals and Escapes”?
Write and escape text safely. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “String Literals and Escapes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Strings Are []const u8
- String Literals and Escapes
- Comparing and Searching Bytes
- Formatting Text with std.fmt