Skip to content
Lesson 5 of 14

Step 1 of 5 · Reading · ~3 min

Learn

Strings

Formatted Printing

By the end of this lesson you can make fmt.Printf lay values out in fixed columns: text padded to a width, numbers right-aligned, and decimals rounded to exactly the places you ask for.

Format strings and verbs

fmt.Printf takes a format string followed by arguments. Ordinary characters in the format string are printed as they are; each verb (a % followed by a letter) is replaced by the next argument, in order.

go
verbprintsexample output
%dan integer42
%sa stringOslo
%fa float, 6 decimals3.140000
%vany value, default form[1 2 3]
%qa string in quotes, escapes visible"hi\n"
%%a literal percent sign%

Printf adds no newline of its own, unlike Println, so end the format string with \n. %q is the verb to reach for when output looks right but fails comparison: it shows a stray \r or tab that %s hides.

Width: a minimum number of columns

A number between % and the verb is the width: the value is padded with spaces until it fills that many columns. Numbers and strings pad on the left (right-aligned) by default. A - flag pads on the right instead (left-aligned).

go

The last line is the rule people get wrong: width is a minimum. A value longer than the width is printed in full and simply pushes the rest of the line to the right. Nothing is ever cut off.

Precision: digits after the point

For floats, .N sets the number of decimals, and the value is rounded, not truncated. Width and precision combine, and the width counts the whole number, including the point and the decimals.

go

A worked example: a scoreboard

A scoreboard row has a player's name left-aligned in 8 columns, points right-aligned in 5, and an accuracy with one decimal followed by a percent sign:

go

Output:

Mo      |  870| 91.3%
Kalinda |12055|  7.0%

Trace the first row: "Mo" is 2 characters, so %-8s adds 6 spaces after it. 870 is 3 digits, so %5d puts 2 spaces before it. 91.27 rounds to 91.3, which is 4 characters, so %5.1f adds 1 space. %% prints the %. Every | lines up because every field has a fixed minimum width.

fmt.Sprintf takes the same verbs but returns the string instead of printing it, which is handy when you want to build a row and print it later.

Common mistakes

  • %f with no precision. 2.25 prints as 2.250000, so a line meant to show money gets four extra digits.
  • Forgetting the - flag. %12s right-aligns, putting the spaces in front of the text: apple instead of apple .
  • Expecting width to cut long values. %-12s on an 18-character name prints all 18 characters. Code that slices the name to fit, or that assumes the columns are always the same position, prints the wrong line.
  • Printing a whole-number float as-is. A price read as 2 must still show as 2.00; only a precision verb guarantees the decimals.

Your exercise

Print one receipt row using a single fmt.Printf with widths and precisions. The starter already reads the item name, quantity and price, computes the total, and prints them with a placeholder Println. The mistake to watch for is a float verb without a precision, which prints six decimals.

Up nextIf, Else, SwitchControl Flow

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…