Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

String formatting combines fixed text with replacement fields, then optionally controls how each value is displayed. In Python, use an f-string such as f"{price:.2f}"; in Rust, use format!("{price:.2}"). The shared ideas—fields, width, alignment and precision—are useful across both languages, but the syntax is not interchangeable.

How a format field works

A replacement field identifies a value and may include a colon followed by presentation instructions. The general pattern is {value:spec}: the value supplies the content, while the specification can control such things as alignment, width, padding, precision, sign and type.

For example, in Python {price:.2f} requests fixed-point output with two digits after the decimal point. In Rust, {price:.2} uses its formatting grammar to request precision; the exact format traits and supported options depend on the value’s type.

Python: f-strings and str.format()

Python’s official tutorial describes formatted string literals, or f-strings, as strings prefixed with f or F that include Python expressions written inside braces: Python tutorial: formatted string literals. Use them when the template is part of the source code:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
name = "Ada"
count = 7
price = 12.5

f"{name} has {count:03d} files"
f"Price: ${price:.2f}"
f"{name!r}"

The !r conversion asks for the value’s repr-style representation before formatting. For a separate template, or when named or positional replacement is convenient, use str.format():

"{name} has {count} files".format(name=name, count=count)

Python’s format-specification reference documents controls including fill, alignment, sign, alternate form, zero padding, width, precision and type: Python format specification mini-language.

Common Python patterns

Pattern What it does Example result
{value} Default formatting Depends on the value
{value!r} Uses repr-style conversion Depends on the value
{n:03d} Formats an integer in width 3 with zero padding 007 for 7
{x:.2f} Fixed-point number with two digits after the decimal point 3.14 for 3.14
{x:.2%} Percentage with two digits after the decimal point 81.66% for 0.8166
{text:<10} Left-aligns within width 10 text
{text:^10} Centers within width 10 text
{n:,} Adds thousands separators 1,000,000

Width sets a minimum field size; alignment determines where the value sits within it. Precision is type-sensitive: for fixed-point numbers, .2f means two digits after the decimal point, while other types may use precision differently.

Rust: the format! macro

Rust’s std::fmt documentation says its arguments use Python-like brace syntax rather than C-style percent markers: Rust formatting documentation. The format! macro builds a String from a literal format string and replacement arguments: Rust format! macro.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
let name = "Ada";
let count = 7;
let price = 12.5;

format!("{name} has {count:03} files");
format!("Price: ${price:.2}");
format!("{:<10}", name);       // left-aligned width 10
format!("{:^10}", name);       // centered width 10
format!("{:?}", name);         // Debug formatting

Rust supports named fields such as {name} and positional fields such as {0} and {1}. Its format grammar also provides fill, alignment (<, ^, >), sign, alternate form, zero padding, width, precision and type controls. Consult the Rust reference for the grammar and formatting traits relevant to a particular value; Python’s exact format codes should not be assumed to work in Rust.

How to choose between them

Need Python Rust
Template written alongside the values Use an f-string, such as f"{name}". Use format!("{name}") to create a String.
Separate template or explicit argument replacement Use str.format(), such as "{name}".format(name=name). Use format! with named or positional arguments.
Align fields or control numeric presentation Use the Python format specification after the colon. Use Rust’s format specification and the value’s supported formatting traits.
Show a debug-style representation Use !r for repr-style conversion. Use {:?} for Debug formatting.

In Python, expressions inside f-string fields are evaluated as part of the string. Avoid treating untrusted text as a format string: distinguish the template from values and understand the evaluation rules of the API you use. In Rust, format! takes a literal format string and arguments; diagnostics and supported formatting are governed by Rust’s compiler and formatting traits.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Print literal braces

Because braces delimit fields, double them when the output needs literal braces. Python f-strings use doubled braces around literal characters:

code = 200
f"{{status: {code}}}"

This produces {status: 200}. Rust explicitly documents the same-character doubling rule for literal braces: Rust format syntax.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
let code = 200;
format!("{{ status: {code} }}");

Practical formatting rules

  • Use width and alignment to make columns easier to scan; they set presentation, not the underlying value.
  • Use zero padding for fixed-width counters or identifiers when leading zeroes are meaningful to the display.
  • Use thousands separators and deliberate decimal precision when they make human-readable numbers clearer.
  • Keep syntax language-specific: learn the shared concepts, then check the official reference for the language and value type you are formatting.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.