Remix.run Logo
blueflow 5 days ago

I fail to see the purpose of f-strings if they end up as complex as printf formatting. Maybe use printf at this point?

ForceBru 4 days ago | parent | next [-]

I think the purpose is to put variables directly in the string literal instead of calling `printf`. Looking at an f-string immediately tells you where each value will be located in the output. `printf` requires you to read the format string, then look at the arguments, then count where a particular value will be printed.

lou1306 4 days ago | parent [-]

`printf`/`str.format` are also prone to nasty failures: if you forget a variable (e.g., `str.format("{} {} {}", 0, 1)`), you only find out when you crash with an IndexError at runtime.

zahlman 4 days ago | parent [-]

I would expect that linters can pick up this sort of thing pretty easily.

Hamuko 4 days ago | parent | prev | next [-]

You don't really need to use any of these. Really the most useful one is f"{var=}" for us print() debuggers. But f"{var:#x}" is the same as f"{hex(var)}", so feel free to pick whatever you prefer.

positr0n 3 days ago | parent | prev [-]

Sometimes you need a formatted string outside of a printf content.