39 comments

[ 0.21 ms ] story [ 48.5 ms ] thread
Cute quiz! As a reminder, if you're doing anything even slightly complicated with f-strings (beyond :x or var=), leave a comment.
21/26, with a couple of sleep-deprived brainfarts and a misclick. Learned very little, although I had the wrong reasoning for one of the ones I got right (tested afterward). I don't even use the {var=} thing or walrus operator, though.

I would definitely not do nearly as well on jsdate.wtf. I really still think JS has the greater WTFs.

I made a mistake on 7, but wouldn't have done if I had syntax highlighting.
Nice quiz! I admit I was surprised a couple of times (especially with f"{1<5:1<5}"), many of these features are hardly ever needed
I like this quiz format much more than just reading a doc because I get to guess what it might do before finding out, which reinforces how well I remember it. As a Python amateur I had no idea fstrings were so powerful!
Just learned about the ellipsis statement and the !a modifier (I already knew !r and !s).
String interpolation is one of those features like inference where if you've had it before then going without is very annoying, and so you add some and that's nicer, then you add more, each step seems like it's an improvement, and then one day you realise you're looking at unintelligible nonsense and you say "Oh no, what have we done?"

This is unusual, often CS would love to have as much of whatever as we can, but mathematics says no that's literally or practically impossible - but here both none and lots are awful and shouldn't be permitted.

One option, which Python and C# both picked is, well, leave it to taste. You can write sixteen pages of layered expressions in the uncommented interpolated string and it'll work, but your colleagues will curse your name and plot your destruction. Or at least you'll fail code review if you enforce such things.

Another option, in standard C++ 23 today for example, is refuse to take even the first step. You can have rich formatting, but standard C++ does not provide interpolation at all, if you want to format six parameters then pass them as parameters.

I'm happy with Rust's "Only a tiny bit of interpolation" where you can interpolate only identifiers, not any other expressions, but that's definitely more interpolation than some will be happy with, yet of course in some cases it's not quite enough.

If this was JavaScript syntax, most of the comments would be lamenting the unintuitive syntax and weird features.
I doesn’t exist. Lol
16/26

I always use a reference when doing anything non-trivial with format strings of any kind and this quiz confirmed I should keep doing that.

Also I've been using Python professionally for over a decade but TIL about the Ellipsis object.

How can I setup a linter to prohibit stuff like this in my code base?
the reward for 100% should be a directory of languages that deals with strings in a sane/singular/readable way.

it's cool that half of those features are there. it's not cool that half the devs that read the thing after creation are going to have to look up the f string features.

> This is the first special feature of f-strings: adding a trailing equals sign lets you print out the expression and what it evaluates to.

    >>> foo='bar'; print(f"{foo=}")
    foo='bar'
Wow, never knew you could do that.
Learned a few tricks that I'm sure are buried on fstring.help somewhere (^ for centering, # for 0x/0b/0o prefixes, !a for ascii). I missed the nested f-strings question, because I've been stuck with 3.11 rules, where nested f-strings are still allowed but require different quote characters (e.g. print(f"{f'{{}}'}") would work). I guess this got cleaned up (along with a bunch of other restrictions like backslashes and newlines) in 3.12.

F-strings are great, but trying to remember the minute differences between string interpolation, old-style formatting with %, and new-style formatting with .format(), is sort of a headache, and there's cases where it's unavoidable to switch between them with some regularity (custom __format__ methods, templating strings, logging, etc). It's great that there's ergonomic new ways of doing things, which makes it all the more frustrating to regularly have to revert to older, less polished solutions.

Why would "0^5" evaluate to 5? (question 21)

And is there a way to link to a specific question?

(comment deleted)
One can question why python needs N built-in ways to printf with different syntax, different ways to escape things and different footguns.
I really like the f"{ varname = }" syntax, didn't know about it before.

I have so much Python to learn, I scored 10/26

I learned a bunch of these when trying to make an f-string-like library for Lua [1], but `f"{...}" and the walrus ones caught me off-guard.

Glad this is nowhere near Wat [2], though.

[1]: https://ezhik.jp/f-string.lua/

[2]: https://www.destroyallsoftware.com/talks/wat

There are many mistakes in the Wat talk from destroyallsoftware. The biggest one is that he types `{}+[]` and claims that he's adding an object and a list. It's actually an empty scope followed by a unary + applied to a list. You can convince yourself of that by noting that `({}+[])` gives different output.

A smaller mistake that I find nonetheless amusing is that he uses Array(16) hoping to get 16 string separators. Oops, off by one error :)

Will there be a companion Tstrings.wtf at some point?
Glorious. I learned a ton, and mostly more than the basics of formatting (something I've never spent a heap of time on). Well done.