TGViewer
Learn Python Coding Learn Python Coding @pythonre · 40.8K subscribers
Post #9080 1.68K
Working with f-strings: more possibilities than it seems!

f-strings often replace .format() in everyday code, but their capabilities are not always fully utilized. They support formatting, function calls, working with data structures, and convenient debugging (from 3.8+).

f-strings are convenient for aligning columns without additional tools. This makes the output readable in the CLI and logs:
rows = [
    ("id", "name", "role"),
    (1, "Ivan", "admin"),
    (2, "Olga", "editor"),
]

for r in rows:
    print(f"{r[0]:<5} {r[1]:<10} {r[2]:<10}")


Debug expressions (Python 3.8+): {x=> displays the name and value of the variable, which speeds up debugging. Supports formatting of calculations:
x = 12
y = 7
print(f"{x=} {y=} {x*y=} x/y={x/y:.3f}")


Specifiers !r, !a: !r - repr(), !a - ascii() for unambiguous logs. Eliminates ambiguities in the output of objects:
path = "/var/data/config.yaml"
print(f"{path!r} {path!a}")  # repr and ascii()


Specifiers support width and padding, for example 08d for zeros. This is convenient for reports and IDs:
n = 42
print(f"{n:08d}")  # → #00000042


You can access dictionaries and immediately calculate metrics, for example len():
data = {"user": "Ivan", "items": [1, 2, 3]}
print(f"{data['user&#39]}=», items={data['items&#39]}")
print(f"len(data['items&#39])={len(data['items&#39])}")


🔥 f-strings are a cool tool for formatting, logging, and debugging, if you apply them taking into account the version of Python and the context of the output.

🚪 @DataScience4
  • ❤ 4
More from @pythonre
  1. Oct 14, 2026Build AI Agents with Google ADK: Gemini, Memory & Vertex AI Build production-ready AI agen…
  2. Oct 13, 2026https://substack.com/@husseinsheikho?utm_source=share&utm_medium=android&r=3hwnr8 Follow m…
  3. Oct 10, 2026📚 Download 10 Python books for FREE: 1. Think Python — O’Reilly https://t.co/X7y3pX68IW 2…
  4. Oct 7, 2026Python Lists: A Quick Reference 🐍 * Definition: Lists are ordered, mutable collections th…
  5. Oct 7, 2026📱 270 tools for Python developers The repository contains: 🫡 Linters and formatters; 🫡…
  6. Oct 7, 2026Hands On Python Data Science - Data Science Bootcamp Master Python for Data Science with R…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →