PYTHON Contents

Strings and f-strings (Real-World Formatting)

Use f-strings, join, and consistent log formatting patterns for readable output without performance and safety pitfalls.

On this page

Production String Formatting

Strings are used in logs, error messages, and identifiers. Consistency improves debugging and reduces noise.

Prefer f-strings

user_id = 42
path = "/api/items"
msg = f"event=request user_id={user_id} path={path}"

Join for Bulk Concatenation

parts = ["alpha", "beta", "gamma"]
s = ",".join(parts)

Operational Checklist

  • Use key=value format in logs to enable parsing.
  • Mask secrets and PII (tokens, emails, phone numbers).
  • Avoid building SQL or shell commands via string concatenation.

Failure Modes

  • Loop + concat: repeated + in loops causes unnecessary allocations.
  • PII leakage: sensitive data ends up in logs.