Strings Are []const u8
Why Zig strings are byte slices.
Strings Are []const u8 is a free Zig Academy lesson on CoddyKit — lesson 1 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.
No String Type
Zig has no dedicated string type. A piece of text is simply a slice of bytes, so you reuse the same tools you already know for arrays. 📦
It Is Just Bytes
A Zig string is the type []const u8: a read-only window over a run of 8-bit bytes. Each byte holds one number from 0 to 255.
const greeting: []const u8 = "hello";Why const
String literals live in read-only memory, so their type is []const u8. The const means you can read the bytes but never write through this slice.
u8 Is One Byte
The element type u8 is an unsigned 8-bit integer. Treating text as u8 makes Zig honest: a string is raw bytes, not magic characters.
A Slice Has a Length
A slice carries both a pointer and a length, so the string knows its own size. You never need a separate length variable or a terminator.
const name = "Zig";
// name.len == 3UTF-8 by Convention
Zig source files are UTF-8, so literals store text as UTF-8 bytes. One emoji or accented letter may take several bytes, not one.
Length Counts Bytes
Because text is bytes, .len returns the byte count, not the character count. For plain ASCII the two numbers match exactly.
const s = "abc";
std.debug.print("{}", .{s.len}); // 3Index Returns a Byte
Indexing a string gives you back a single u8 byte value, not a character object. For ASCII, that byte is the letter is code.
const s = "AB";
const first = s[0]; // 65, the ANo Null Terminator Needed
Unlike C, a Zig slice does not rely on a trailing zero byte to mark its end. The stored length already tells code where text stops.
Print a String
You print text with the {s} format specifier, which tells Zig to render the bytes as a string instead of as numbers.
std.debug.print("{s}\n", .{"hello"});Mutable Bytes Need [] u8
Drop the const to get a writable []u8 slice. Then you can change bytes in place, as long as they live in memory you own.
Quick Check
Think about the real type behind a Zig string literal.
Recap
You learned that Zig text is just []const u8: read-only bytes with a length, UTF-8 by convention, indexed and measured in bytes. 🎯
Frequently asked questions
Is the “Strings Are []const u8” lesson free?
Yes — the full text of “Strings Are []const u8” 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 “Strings Are []const u8”?
Why Zig strings are byte slices. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Strings Are []const u8” 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