Python F-Strings Number Formatting Cheat Sheet
cheatography.com
cheatography.com
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.99And 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".
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.
Rather than using strftime
https://towardsdatascience.com/python-f-strings-are-more-pow...
print(f”...”)
I’m not sure if this was intentional or not but it is very tastefully designed either way.Space on a website is free, whats with the confusing newspaper layout??
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}")