frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Fstrings.wtf

https://fstrings.wtf/
243•darkamaul•5h ago

Comments

underdeserver•4h ago
Cute quiz! As a reminder, if you're doing anything even slightly complicated with f-strings (beyond :x or var=), leave a comment.
zahlman•4h ago
Ohg abg orgjrra gur oenprf! ;)
Disposal8433•4h ago
As someone who works on a codebase with almost no comments, please leave a comment for every block of code that does something and explain why (1 comment for every 5 or 10 lines of code should be fine).

And for regex you have this in some languages: https://docs.python.org/3/library/re.html#re.X

cluckindan•2h ago
Please include any technical and especially business reasons in the ”why”, please.
elteto•4h ago
Hey, I’m doing complicated things with f-strings so I’m leaving a comment as requested.
zahlman•4h ago
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.

the_mitsuhiko•4h ago
I was not sure about the difficulty. Python has some really weird behaviors with some of the custom __format__. For instance f"{'None':<010}" will pad out, but f"{None:<010}" will error. The only one I ended up putting in is the gotcha that bool is an int subclass and gets the int behavior for custom format parameters (which almost sounds like a bug). f'{''''''''''''}' is also a weird one, but that is mostly an issue with the string concatenation syntax and not fstrings :)

There definitely are some more odd / confusing ones.

OJFord•4h ago
> For instance f"{'None':<010}" will pad out, but f"{None:<010}" will error.

Is that any different than being surprised that 1 + 1 is 2 and '1' + '1' is '11'?

lionkor•3h ago
not really, since None stringifies to "None", so you would expect None -> "None" -> padding and whatnot added
the_mitsuhiko•3h ago
I don't know, but I find this quite confusing:

    >>> f"{'42':<10}"
    '42        '
    >>> f"{42:<10}"
    '42        '
    >>> f"{None:<10}"
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported format string passed to NoneType.__format__
(Or any other type. What can be padded is quite inconsistent)
OJFord•2h ago
Ah I see, fair enough I suppose. Quite nice for catching bugs though to be honest - since you'll hit an error and realise you need to handle the None case appropriately, rather than a more subtle 'None' appearing somewhere bug.
Retr0id•4h ago
I made a mistake on 7, but wouldn't have done if I had syntax highlighting.
istrice•4h ago
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
Liftyee•4h ago
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!
frickit•3h ago
These games just reinforce that I’m an idiot.
meinersbur•4h ago
Just learned about the ellipsis statement and the !a modifier (I already knew !r and !s).
tialaramex•4h ago
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.

Waterluvian•2h ago
Purity and practicality are at odds and every language finds a different balance between the two. There is no one correct balance so busy minds will inevitably have loud opinions they want accepted as the one correct balance.
almostgotcaught•17m ago
> busy minds

cute pun but compared to busybodies it really hides the implication (i thought you were talking about people with adhd at first).

sublinear•1h ago
> CS would love to have as much of whatever as we can, but mathematics says no

What does this have to do with either topic?

veber-alex•38m ago
I hate Rust's solution because it's not a solution at all.

Interpolation only works in a small subset of cases, which makes you constantly having to think whether it can or can't be used in the current situation and requires endless churn when refactoring code.

At the very minimum, they need to allow it to work with field access.

On the other hand, in python, while examples like this site exist and are funny/weird/quirky in practice nobody cares, and they just enjoy using fstrings.

almostgotcaught•38m ago
> String interpolation is one of those features

70% of these "wtfs" aren't about string interpolation but just python's syntax for string.format

https://docs.python.org/3/library/string.html#format-string-...

cluckindan•4h ago
If this was JavaScript syntax, most of the comments would be lamenting the unintuitive syntax and weird features.
crazygringo•3h ago
Right, I assumed the real point the quiz was making is that Python is as full of footguns as JavaScript, since I've seen this type of thing for JS a bunch of times.

Not saying I agree, but was definitely expecting that to be the main topic of discussion here...

jvolkman•2h ago
But if it were Perl, they'd be celebrating.
WithinReason•4h ago
thanks I hate Python now
kosolam•3h ago
I doesn’t exist. Lol
psychoslave•3h ago
Awesome, makes me glad I didn't touch Python for years now.

Hell is paved with good will I guess. Probably Justine should update https://justine.lol/lex/

skrebbel•3h ago
> makes me glad I didn't touch Python for years now.

C'mon, every language has quirks.

bogtog•2h ago
Many of these are desirable features!
CivBase•3h ago
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.

neoden•3h ago
How can I setup a linter to prohibit stuff like this in my code base?
hackyhacky•3h ago
Just use Python 3.5.
jwilk•2h ago
str.format() can be cursed too, e.g.:

  >>> '{:{}{}}'.format('m', 'o<2', 3)
  'moooooooooooooooooooooo'
serf•3h ago
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.

Y_Y•2h ago
I've compiled a directory of those lamguages here:
nojs•3h ago
> 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.
black_puppydog•3h ago
such a boon for print(f) debugging. :)
jszymborski•1h ago
No more will I have to

print("foo", foo)

ck45•3h ago
Python release notes are really worth reading, for me there's usually some "positive surprise"

The = support was added in Python 3.8: https://docs.python.org/3/whatsnew/3.8.html#f-strings-suppor...

carlhjerpe•1h ago
If you come from verbosity land C# release notes are magically good as well, always some way to reduce boilerplate while maintaining "implicit verbosity" which your proprietary LSP resolves 100% correctly.

I'd prefer writing C# if I had the Linux interaction libs Python has. I'm too dumb to write syscall wrappers

ahartmetz•2h ago
Incredibly common for debug output. In C++, I have made it a habit to just copy the expression, once with quotes and once without. It's informative and doesn't require thinking, or, well, I'm still working on that.
stkdump•2h ago
It's the kind of thing you do with macros in C++.
acdha•1h ago
It makes me sad that the PEP for the equivalent behaviour for function keyword arguments wasn’t accepted. It’s really common to see foo(bar=bar) and I think it’s not only cleaner but would help see subtle differences if that was foo(bar=) because it would make the cases where some arguments aren’t simply being passed through more obvious: foo(bar=, …, baaz=baaz.get_id()) avoids the most interesting detail being easily missed.
carlhjerpe•1h ago
Do you know why? I didn't know of the fstring one either but I've thought to myself across many languages that a way to print the expression (or just varname in my head) with the result should exist.
akubera•25m ago
The PEP: https://peps.python.org/pep-0736/

The discussion: https://discuss.python.org/t/pep-736-keyword-argument-shorth...

The rejection: https://discuss.python.org/t/pep-736-shorthand-syntax-for-ke...

Grammar changes, in particular things used everywhere like function invocations, have to be worth paying the price for changing/adding new rules. The benefits of fewer characters and more explicit intention weren't enough to outweigh the costs.

There were other considerations: Do linters prefer one syntax to another? Does the name refer to the parameter or the argument in tooling? Should users feel pressure to name local variables the same as the function's parameters? What about more shorthand for common cases like func(x=self.x, y=self.y)?

I personally did not like the func(x=, y=) syntax. I think their example of Ruby's func(x:, y:) would actually make more sense, since it's syntax that would read less like "x equals nothing", and more "this is special syntax for passing arguments".

roenxi•1h ago
And it seems like a bad idea because of that wow factor - it isn't really adding enough to justify having surprising behaviour. It is more likely to be a bug than a feature.

It'd be better to just let people implement their own function that prints a subset of locals(), or provide a standard function that does the same.

stephenlf•3h ago
Much easier than date.wtf
ciupicri•30m ago
From JavaScript i.e. https://jsdate.wtf/ ?
ejiblabahaba•3h ago
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.

sfoley•1h ago
Yeah I consider that one to be a trick question. I knew same-quote-style nested f-strings were coming, I just didn't know which version, and I still use the `f'{f"{}"}'` trick because I want my code to support "older" versions of python. One of my servers is still on 3.10. 3.11 won't be EOL until 2027.
jdranczewski•2h ago
I usually refer to https://pyformat.info/, which doesn't have all this detail, but most of the reasonable stuff is included
jvdvegt•2h ago
Why would "0^5" evaluate to 5? (question 21)

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

andrewfurey2003•2h ago
xor
palotasb•2h ago
^ is the XOR operator, 0 XOR 5 is 5. (Exponentiation is *)

https://docs.python.org/3/library/operator.html#mapping-oper...

teddyh•1h ago
Exponentiation is ** or pow()
procaryote•2h ago
One can question why python needs N built-in ways to printf with different syntax, different ways to escape things and different footguns.
6thbit•58m ago
“ There should be one-- and preferably only one --obvious way to do it.” — Zen of Python
pansa2•31m ago
> many recent Python extensions seem features in search of use cases […] The real problem with Python, of course, is that its evolution is largely driven by narcissism, not user feedback.

Mark Lutz

https://learning-python.com/python-changes-2014-plus.html

carlhjerpe•1h ago
I really like the f"{ varname = }" syntax, didn't know about it before.

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

Ezhik•1h ago
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

travisgriggs•55m ago
Will there be a companion Tstrings.wtf at some point?
tomrod•53m ago
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.
Twey•43m ago
As someone who hasn't written Python in anger since before f-strings were a thing, I correctly guessed almost all the f-string specific syntax but made a bunch of errors that were just to do with the return values of various expressions. Maybe f-strings are the least wtf thing about Python? :)
pansa2•42m ago
Despite the URL, I’d only consider a few of these to be WTFs. Questions 20 & 21 definitely are, though:

  >>> a = 42
  >>> print(f"{a:=10}")
          42
  >>> print(f"{(a:=10)}")
  10
I still can’t believe anyone thought the walrus operator was a good idea.
almostgotcaught•36m ago
this has nothing to do with the walrus operator and everything to do with how string.format works in python https://docs.python.org/3/library/string.html#format-string-...

Edit: someone downvoted me because they don't understand there is no walrus operator here

    print(f"{a:=10}")
jan_Sate•36m ago
Woah. That's tough. I scored 12/26.

I'm surprised to find that there're so many feature of f-string that I've never heard of. I don't think I'm gonna use them any time soon but nice to know about that.

noisy_boy•5m ago
16/26 I thought I knew f-strings well, but evidently I don't. I am still thinking in 3.6 mode.

Nobody knows how to build with AI yet

https://worksonmymachine.substack.com/p/nobody-knows-how-to-build-with-ai
35•Stwerner•39m ago•15 comments

Known Bad Email Clients

https://www.emailprivacytester.com/badClients
14•mike-cardwell•26m ago•12 comments

Linux and Secure Boot certificate expiration

https://lwn.net/SubscriberLink/1029767/43b62a7a7408c2a9/
90•todsacerdoti•7h ago•39 comments

Fstrings.wtf

https://fstrings.wtf/
247•darkamaul•5h ago•69 comments

My Self-Hosting Setup

https://codecaptured.com/blog/my-ultimate-self-hosting-setup/
403•mirdaki•13h ago•147 comments

Hyatt Hotels are using algorithmic Rest “smoking detectors”

https://twitter.com/_ZachGriff/status/1945959030851035223
378•RebeccaTheDev•12h ago•210 comments

Babies made using three people's DNA are born free of mitochondrial disease

https://www.bbc.com/news/articles/cn8179z199vo
91•1659447091•2d ago•46 comments

Valve confirms credit card companies pressured it to delist certain adult games

https://www.pcgamer.com/software/platforms/valve-confirms-credit-card-companies-pressured-it-to-delist-certain-adult-games-from-steam/
725•freedomben•1d ago•701 comments

OpenAI claims Gold-medal performance at IMO 2025

https://twitter.com/alexwei_/status/1946477742855532918
153•Davidzheng•7h ago•233 comments

A 14kb page can load much faster than a 15kb page (2022)

https://endtimes.dev/why-your-website-should-be-under-14kb-in-size/
335•truxs•7h ago•229 comments

Piramidal (YC W24) Is Hiring a Full Stack Engineer

https://www.ycombinator.com/companies/piramidal/jobs/JfeI3uE-full-stack-engineer
1•dsacellarius•4h ago

Pimping My Casio: Part Deux

https://blog.jgc.org/2025/07/pimping-my-casio-part-deux.html
107•r4um•8h ago•30 comments

I avoid using LLMs as a publisher and writer

https://lifehacky.net/prompt-0b953c089b44
129•tombarys•5h ago•78 comments

YouTube No Translation

https://addons.mozilla.org/en-US/firefox/addon/youtube-no-translation/
116•thefox•8h ago•55 comments

Advertising without signal: The rise of the grifter equilibrium

https://www.gojiberries.io/advertising-without-signal-whe-amazon-ads-confuse-more-than-they-clarify/
130•neehao•13h ago•56 comments

How to write Rust in the Linux kernel: part 3

https://lwn.net/SubscriberLink/1026694/3413f4b43c862629/
229•chmaynard•17h ago•16 comments

Asynchrony is not concurrency

https://kristoff.it/blog/asynchrony-is-not-concurrency/
275•kristoff_it•21h ago•194 comments

Astronomers use colors of trans-Neptunian objects to track ancient stellar flyby

https://phys.org/news/2025-07-astronomers-trans-neptunian-track-ancient.html
12•bikenaga•3d ago•4 comments

Meta says it won’t sign Europe AI agreement, calling it an overreach

https://www.cnbc.com/2025/07/18/meta-europe-ai-code.html
288•rntn•22h ago•385 comments

N78 band 5G NR recordings

https://destevez.net/2025/07/n78-band-5g-nr-recordings/
9•Nokinside•2d ago•0 comments

What is the richest country in 2025?

https://www.economist.com/graphic-detail/2025/07/18/what-is-the-richest-country-in-the-world-in-2025
9•RestlessMind•39m ago•4 comments

A CarFax for Used PCs: Hewlett Packard wants to give old laptops new life

https://spectrum.ieee.org/carfax-used-pcs
20•miles•3d ago•20 comments

Zig Interface Revisited

https://williamw520.github.io/2025/07/13/zig-interface-revisited.html
8•ww520•2d ago•1 comments

An exponential improvement for Ramsey lower bounds

https://arxiv.org/abs/2507.12926
17•IdealeZahlen•6h ago•1 comments

Debcraft – Easiest way to modify and build Debian packages

https://optimizedbyotto.com/post/debcraft-easy-debian-packaging/
70•pabs3•16h ago•22 comments

Bun adds pnpm-style isolated installation mode

https://github.com/oven-sh/bun/pull/20440
97•nateb2022•15h ago•15 comments

Mr Browser – Macintosh Repository file downloader that runs directly on 68k Macs

https://www.macintoshrepository.org/44146-mr-browser
78•zdw•16h ago•17 comments

Broadcom to discontinue free Bitnami Helm charts

https://github.com/bitnami/charts/issues/35164
201•mmoogle•20h ago•108 comments

Silence Is a Commons by Ivan Illich (1983)

http://www.davidtinapple.com/illich/1983_silence_commons.html
178•entaloneralie•19h ago•45 comments

Zig's New Writer

https://www.openmymind.net/Zigs-New-Writer/
90•Bogdanp•2d ago•13 comments