Python String Formatting: f-strings vs .format() vs % (slicker.me)
from monica_b1998@lemmy.world to python@programming.dev on 02 May 2026 03:16
https://lemmy.world/post/46322810

#python

threaded - newest

charokol@lemmy.world on 02 May 2026 03:29 next collapse

Didn’t think I’d learn anything new in this article, but I never knew about f-string’s self-documenting expression syntax. I’ve been doing it myself like a chump!

monica_b1998@lemmy.world on 02 May 2026 03:38 collapse

lol, don’t be too hard on yourself!

charokol@lemmy.world on 02 May 2026 03:44 collapse

I’m not! I’m excited to forget this exists the next time it would be convenient for me to remember it 😋

itsathursday@lemmy.world on 02 May 2026 07:29 next collapse

What annoyed me moving from % to format was the loss of “formatting” floats to integers with “{:d}”, now you get a ValueError

ZoteTheMighty@lemmy.zip on 02 May 2026 17:04 collapse

{:.0f} always works though for floats or ints

itsathursday@lemmy.world on 03 May 2026 07:57 collapse

Ah nice one

Stefan_S_from_H@piefed.zip on 02 May 2026 09:56 next collapse

Formatting strings, the running gag of the Python community.

slag@lemmy.dbzer0.com on 02 May 2026 11:10 next collapse

And here I thought f-strings were something that happen when others are forced to read my code.

olafurp@lemmy.world on 02 May 2026 11:18 next collapse

I worked in python when f-strings were pretty new. Didn’t realise why you would use anything else. It just reads better.

Michal@programming.dev on 02 May 2026 13:33 collapse

Most importantly you can’t use them with translated strings, so it’s not usable for user facing strings unless you don’t care. This limits fstring usefulness a lot in the projects I work on.

Badabinski@kbin.earth on 02 May 2026 13:56 collapse

Luckily, there's now t-strings (e.g. t"{buh}") for that use case (among many others). Here's the PEP: https://peps.python.org/pep-0750/

NotSteve_@lemmy.ca on 02 May 2026 14:33 collapse

F-strings and .format() are two features I miss from Python whenever I’m writing stuff in another language. The syntax is just so minimal yet powerful

Vulwsztyn@programming.dev on 04 May 2026 14:51 collapse

in what language?

NotSteve_@lemmy.ca on 04 May 2026 15:16 collapse

Go mainly. fmt has some functions to do the same sort of formatting but I loved the minimal syntax in Python. JavaScript has a really nice syntax for it as well actually

Vulwsztyn@programming.dev on 04 May 2026 22:10 collapse

reasonable answer