Remix.run Logo
Mawr 8 days ago

Sure, here are the two Go/C-style formatting options:

    fmt.Sprintf("This house is %s tall", measurements(2.5))

    fmt.Sprint("This house is ", measurements(2.5), " tall")
And the Python f-string equivalent:

    f"This house is {measurements(2.5)} tall"
The Sprintf version sucks because for every formatting argument, like "%s", we need to stop reading the string and look for the corresponding argument to the function. Not so bad for one argument but gets linearly worse.

Sprint is better in that regard, we can read from left to right without interruptions, but is a pain to write due to all the punctuation, nevermind refactor. For example, try adding a new variable between "This" and "house". With the f-string you just type {var} before "house" and you're done. With Sprint, you're now juggling quotation marks and commas. And that's just a simple addition of a new variable. Moving variables or substrings around is even worse.

Summing up, f-strings are substantially more ergonomic to use and since string formatting is so commonly done, this adds up quickly.

throwaway2037 8 days ago | parent [-]

    > Not so bad for one argument but gets linearly worse.
This is a powerful "pro". Thanks.