How to do string formatting with placeholders in Java (like in Python)?

The MessageFormat class looks like what you're after.

System.out.println(MessageFormat.format("{0} + {1} = {2}", x, y, x + y));

Slf4j has MessageFormatter.format() that accepts {} without the argument number, just like Python. Slf4j is a popular logging framework, but you don't have to use it for logging to use MessageFormatter.


Java has a String.format method that works similarly to this. Here's an example of how to use it. This is the documentation reference that explains what all those % options can be.

And here's an inlined example:

package com.sandbox;

public class Sandbox {

    public static void main(String[] args) {
        System.out.println(String.format("It is %d oclock", 5));
    }        
}

This prints "It is 5 oclock".