Menu

Java String Format: String.format, printf, and Format Specifiers

How to format strings in Java with String.format and printf - the format specifiers for numbers, padding, decimals, and the text blocks and formatted method.

This page includes runnable editors - edit, run, and see output instantly.

Two Ways to Format

Java formats strings from a template containing format specifiers (the % placeholders) plus the values to drop in. There are two entry points, and they share identical syntax:

  • String.format(template, values...) returns a new String.
  • System.out.printf(template, values...) prints the result directly.

Both produce the same text. String.format hands it back so you can store it; printf writes it to the console. Note the %n at the end of the printf template - that is the platform-independent newline (prefer it over \n in format strings).

The Common Specifiers

Each specifier starts with % and ends with a letter that says what kind of value to expect:

The everyday three are %s (any value, via its toString), %d (whole numbers), and %f (decimals). Use %% to print a literal percent sign. Passing the wrong type - say a String to %d - throws an IllegalFormatConversionException at run time.

Controlling Decimal Places

%f defaults to six decimal places, which is rarely what you want. Put .N before the f to fix the precision (the value is rounded):

Add a comma flag for grouping separators on large numbers:

Width and Padding

A number between the % and the type letter sets a minimum field width, padding with spaces so columns line up. A leading - left-aligns; a leading 0 pads numbers with zeros:

This is what makes formatted tables and aligned reports readable, especially when you print each row inside a for loop:

The %-8s left-aligns names in an 8-wide column and %5d right-aligns scores in a 5-wide column, so the numbers line up regardless of digit count.

Argument Order

By default specifiers consume values left to right. You can repeat or reorder a value with N$ (one-based index), handy when the same value appears twice:

%1$s refers to the first argument both times, so echo prints twice.

The formatted() Method

Since Java 15, every string has a formatted instance method - the same engine as String.format, just written as a method call on the template:

It pairs especially well with text blocks (triple-quoted multi-line strings) when building larger formatted output.

When Not to Use String.format

For simple one-off joins, plain concatenation with + is clearer and faster:

String greeting = "Hi, " + name + "!";   // simpler than String.format here

Reach for String.format / printf when you need real formatting - fixed decimal places, padding, alignment, grouping - not just gluing a couple of values together.

Next: Operators

Formatting turns values into text. To compute those values in the first place you need Java's operators - arithmetic, comparison, and logical. That is the next page.

Frequently Asked Questions

How do you format a string in Java?

Use String.format(template, values...), which returns a new string with each format specifier replaced by a value: String.format("%s is %d", name, age). To print directly instead of building a string, use System.out.printf(...) with the same template. Since Java 15 you can also call template.formatted(values...).

How do you format a number to two decimal places in Java?

Use the %.2f specifier: String.format("%.2f", 3.14159) produces "3.14". The number after the dot is how many decimal places to keep, and the value is rounded. For thousands separators add a comma flag: %,.2f gives "1,234.57".

What is the difference between String.format and printf in Java?

They use the exact same format specifiers. String.format(...) returns the formatted text as a new String you can store or pass around. System.out.printf(...) writes the formatted text straight to standard output and returns nothing useful. Use String.format to build a string, printf to print one.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED