Python
string formatting
multiple arguments
string interpolation
programming tips

Using multiple arguments for string formatting in Python e.g., 's ... s'

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Python has several ways to format strings with multiple values: percent formatting, str.format, and f-strings. All three work, but they differ in readability, type handling, and maintainability in large codebases. The practical goal is not just making text output appear, but choosing a style that stays clear when templates evolve.

Percent Formatting with Multiple Values

Percent formatting is the oldest style and still appears in legacy code. When formatting multiple values, pass a tuple on the right side.

python
1name = "Alex"
2age = 31
3score = 93.7
4
5text = "Name: %s | Age: %d | Score: %.1f" % (name, age, score)
6print(text)

This style is concise, but placeholder mistakes can be hard to debug in complex templates.

str.format with Positional and Named Fields

str.format is more explicit and supports both positional and named placeholders.

python
1city = "Toronto"
2temp = 22
3humidity = 0.61
4
5msg_positional = "City: {}, Temp: {}C, Humidity: {:.0%}".format(city, temp, humidity)
6msg_named = "City: {c}, Temp: {t}C, Humidity: {h:.0%}".format(c=city, t=temp, h=humidity)
7
8print(msg_positional)
9print(msg_named)

Named fields are especially useful when templates have many values, because placeholder meaning remains obvious.

F-Strings as the Modern Default

F-strings are typically the clearest option in modern Python. They support expressions, formatting specifiers, and direct variable interpolation.

python
1product = "SSD"
2unit_price = 129.99
3qty = 3
4
5invoice = f"Product: {product} | Qty: {qty} | Total: ${unit_price * qty:.2f}"
6print(invoice)

This style reduces verbosity and is easy to read during code review.

Formatting Many Fields from Collections

When values come from dictionaries or objects, named formatting avoids positional confusion.

python
1record = {
2    "name": "Maya",
3    "team": "infra",
4    "tickets": 12,
5}
6
7line = "Name: {name}, Team: {team}, Tickets: {tickets}".format(**record)
8print(line)

Equivalent f-string style works well when variable names are already local:

python
1name = record["name"]
2team = record["team"]
3tickets = record["tickets"]
4
5line2 = f"Name: {name}, Team: {team}, Tickets: {tickets}"
6print(line2)

Reusable Templates and Helper Functions

For repeated output patterns, wrap formatting in helper functions. This keeps call sites clean and centralizes template changes.

python
1from datetime import datetime
2
3
4def format_order(order_id: int, amount: float, created: datetime) -> str:
5    return (
6        f"Order #{order_id:05d} | "
7        f"Amount: ${amount:,.2f} | "
8        f"Created: {created:%Y-%m-%d %H:%M}"
9    )
10
11
12print(format_order(73, 1842.5, datetime(2026, 3, 4, 14, 25)))

A helper function also makes unit tests straightforward, which matters for invoices, logs, and user-visible messages.

Selecting a Style for Team Code

Reasonable team guideline:

  • prefer f-strings for new code.
  • use str.format for reusable external templates or dynamic placeholder maps.
  • keep percent formatting only where legacy compatibility requires it.

Consistency matters more than personal preference. Mixing styles randomly inside one module increases cognitive load.

Logging and Localization Notes

For structured logs, prefer passing raw values to logger fields instead of pre-formatting long strings early. For user-facing localized text, named placeholders are easier for translators and reduce mistakes when argument order differs between languages.

Common Pitfalls

  • Forgetting tuple parentheses in percent formatting with multiple values.
  • Mismatching format specifier and data type, such as %d with non-integers.
  • Overusing complex expressions directly inside f-strings, hurting readability.
  • Mixing too many formatting styles in the same file.
  • Building user-facing text without reusable helpers, making later edits error-prone.

Summary

  • Python supports multiple-value string formatting through three major styles.
  • Percent formatting works but is mostly legacy in modern projects.
  • 'str.format is flexible for named and reusable templates.'
  • F-strings are usually the clearest default for current Python code.
  • Centralize formatting logic when output rules are business-critical.

Course illustration
Course illustration

All Rights Reserved.