5 ms·
Python F-Strings Number Formatting Cheat Sheet
- RandomWorker 4y agohttps://pyformat.info https://pyformat.info this still remains my favourite place to go for doing a great print. :)
- kemayo 4y agoThough regrettably it's out of date and doesn't cover f-strings at all.
- kkirsche 4y agoThe issue tracker though includes https://fstring.help https://fstring.help if it helps :)
- BerislavLopac 4y agoOf course it does - f-strings follow the same syntax as the format method.
- pbourke 4y ago“=“ has been a great addition to F-strings. It allows you to reduce duplication for labeled output. For example: count = 10 size = 1000032 area = 3.14159 * (32 ** 2) print(f"{count=} {size=:,} {area=:,.2f}") will output this count=10 size=1,000,032 area=3,216.99
- ajross 4y agoCounterargument: "=" is YAMLization. It prioritizes concision and an abstracted ideal of "readability"[1] at the expense of mental model and ease of use. Every non-obvious feature like this has cost. Every user who sees this for the first time is going to be confused. Every time a language or syntax adds a "shorthand" feature like this it makes itself less accessible. And in this case I think the value is really pretty low. That particular "=" syntax is really mostly just useful for dumping runtime state at debug time. And the world is filled with tools that can do that for you with even more concision. [1] Which doesn't really correspond to general comprehension. It means something more like "readability for experts".
- pbourke 4y ago> It prioritizes concision and an abstracted ideal of "readability"[1] at the expense of mental model and ease of use. That statement applies to all format strings, which are by necessity a small internal DSL. For the = syntax, the input is copied directly to the output with the value of the variable appended. Within the realm of language features, sure it's one more chocolate wafer of syntax. Within the realm of format strings, it's certainly more straightforward than the type conversion and padding syntax. > That particular "=" syntax is really mostly just useful for dumping runtime state at debug time. In scientific / ML development that's like 80% of the work.
- deleted 4y ago[deleted]
- pantsforbirds 4y agoIt's an extremely useful pattern for me when logging to be honest.
- pantsforbirds 4y agoThis is honestly my favorite formatting feature. I use it a lot in debug/info logs and its honestly my first go to when debugging something trivial enough that I don't want to spin up the full debugger.
- blooalien 4y agoI use this regularly, combined with Wil McGugan's `rich.print` for easy fancy colorized output. f-strings are one of my favorite things by far to happen to Python in recent memory, along with the robust Unicode support and fancy new `async` stuff. :)
- shantnutiwari 4y agoSorry, that's terrible UX. I can't follow whats going on. And is it just me, or do only some cases have examples? Space on a website is free, whats with the confusing newspaper layout??
- mahdi7d1 4y agoI think the author focused more on the PDF version and just translated his design for PDF to the website.
- tafazi9090 4y ago“Cheat sheet” but shoved a cookie popup in my face. No thank you
- jl6 4y agoVery useful. I’ve always thought of Python f-strings as being a language within a language, and not a terribly pythonic one.
- pantsforbirds 4y agoI'm rather curious about the "non pythonic" part of your comment. The % formatting to me has always felt like a compatibility thing with C and I've always found the {} format style much more pythonic. f strings seem like a logical extension/continuation of that formatting style into something that works really quite well with python in my personal experience. As an example I think each iteration of formatting is more readable and arguably more pythonic: log.info("%s is logged into service #%d" % (user_name, service_number)) log.info("{} is logged into service #{}".format(user_name, service_number)) log.info(f"{user_name} is logged into service #{service_number}")
- jl6 4y agoI guess I was thinking about examples like f"{3:0=+5}".
- mahdi7d1 4y agoI wonder why I forget these all the time and have to search for them at least once a month.
- dehrmann 4y agoI'm amazed at how much use I've gotten out of learning C printf format strings. A lot of these format specifiers are the same, Java also uses them, and I'm sure more languages took inspiration from them.
- zikohh 4y agoThis one wasn't on the cheat sheet but here is my favourite f"{datetime.utcnow():%Y-%m-%d}" Rather than using strftime https://towardsdatascience.com/python-f-strings-are-more-powerful-than-you-might-think-8271d3efbd7d https://towardsdatascience.com/python-f-strings-are-more-pow...
- blooalien 4y agoThank you for that one. I also like the shortcut date/time format specifiers %F, %T, %x, and %X they list there, too. Great link. Bookmarked…
- gorgoiler 4y agoI’ve always appreciated how, in place of a C-like language’s printf, one instead writes: print(f”...”) I’m not sure if this was intentional or not but it is very tastefully designed either way.