Formatting Text with std.fmt
Build strings with format specifiers.
Formatting Text with std.fmt is a free Zig Academy lesson on CoddyKit — lesson 4 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.
Why Formatting
Real programs stitch values into text: names, counts, prices. Zig handles this through std.fmt and its format-string mini-language.
Placeholders in Braces
A format string uses {} as a placeholder. Each pair of braces pulls the next argument from the tuple you pass alongside it.
std.debug.print("x = {}\n", .{42});Arguments Go in a Tuple
You supply arguments as an anonymous tuple, written .{ a, b }. Even a single argument lives inside that tuple wrapper.
std.debug.print("{} {}\n", .{1, 2});String Specifier
Print a []const u8 as text with {s}. A bare {} would try to format the slice as a struct of pointer and length instead.
std.debug.print("{s}\n", .{"hi"});Number Bases
Control an integer is base with specifiers like {x} for hex, {b} for binary, and {o} for octal. Great for low-level debugging.
std.debug.print("{x}\n", .{255}); // ffWidth and Padding
Pad a value to a fixed width using a colon, as in {d:5}. It lines numbers up neatly into clean, readable columns.
std.debug.print("{d:5}\n", .{7});Float Precision
Limit decimal places with a precision field like {d:.2}. This prints exactly two digits after the point for a tidy result.
std.debug.print("{d:.2}\n", .{3.14159});Escape a Brace
Need a literal curly brace in your output? Double it: {{ prints one { and }} prints one }. This avoids placeholder confusion.
std.debug.print("{{x}}\n", .{});Build a String
To format into memory instead of stdout, call std.fmt.allocPrint. It allocates and returns a fresh []u8 holding your text.
const s = try std.fmt.allocPrint(
alloc, "id={}", .{id});Free What You Allocate
Because allocPrint uses an allocator, you own the result and must free it. A defer right after the call keeps cleanup tidy.
defer alloc.free(s);Fixed Buffers with bufPrint
When you want no heap at all, write into your own array with std.fmt.bufPrint. It returns the filled-in slice it actually used.
var buf: [16]u8 = undefined;
const out = try std.fmt.bufPrint(&buf, "{}", .{9});Quick Check
You are printing a []const u8 value and want it shown as readable text.
Recap
You formatted text with std.fmt: {} and {s} placeholders, base, width, and precision, plus allocPrint and bufPrint for strings. 🎨
Frequently asked questions
Is the “Formatting Text with std.fmt” lesson free?
Yes — the full text of “Formatting Text with std.fmt” 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 “Formatting Text with std.fmt”?
Build strings with format specifiers. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Formatting Text with std.fmt” 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